-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamsExample.java
More file actions
125 lines (97 loc) · 4.91 KB
/
Copy pathStreamsExample.java
File metadata and controls
125 lines (97 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package java8;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamsExample {
// Given a list of integers, find out all the numbers starting with 1 using
// Stream functions.
public static List<Integer> getNumbersStartsWithOne(List<Integer> numbers) {
return numbers.stream().filter(x -> x.toString().startsWith("1"))
.collect(Collectors.toList());
}
// find duplicate elements in the list.
public static List<Integer> findDuplicateElements(List<Integer> list) {
Set<Integer> tempSet = new HashSet<>();
return list.stream().filter(x -> !tempSet.add(x)).collect(Collectors.toList());
}
// find largest element of the list using streams.
public static Integer findLargestElement(List<Integer> list) {
Integer largest = list.stream().reduce(Integer.MIN_VALUE, Integer::max);
return largest;
// return list.stream().max(Integer::compareTo).orElse(null);
}
// ���nd smallest element of the list using streams.
public static Integer findSmallestElement(List<Integer> list) {
Integer smallest = list.stream().reduce(Integer.MAX_VALUE, Integer::min);
return smallest;
}
// find largest element of the list using streams by comparing to Integer
// argument.
public static Integer findLargestElementByComparing(List<Integer> list, int target) {
return list.stream().filter(x -> x > target).max(Integer::compareTo).orElse(null);
// return list.stream().max(Integer::compareTo).orElse(null);
}
public static char findFirstNonRepeatativeCharacter(String s1) {
return s1.chars().mapToObj(x -> (char) x).filter(x -> s1.indexOf(x) == s1.lastIndexOf(x)).findFirst()
.orElse('0');
}
public static void main(String[] args) {
// Given a list of integers, find out all the numbers starting with 1 using
// Stream functions.
List<Integer> numbers = getNumbersStartsWithOne(Stream.of(7, 2, 9, 1, 5, 11).collect(Collectors.toList()));
System.out.println(numbers);
// Output: [1, 11]
int[] arr = { 7, 2, 9, 1, 5, 11, 123 };
int[] res = Arrays.stream(arr)
.filter(s -> String.valueOf(s).startsWith("1"))
.toArray();
System.out.println(Arrays.toString(res));
// Output: [1, 11, 123]
// find duplicate elements in the list.
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3);
List<Integer> duplicateElements = findDuplicateElements(list);
System.out.println("Duplicate Elements :: " + duplicateElements);
// Output: [1, 2, 3]
// Using parallel streams
// List<Integer> duplicateElementsParallel = list.parallelStream()
// .filter(x -> !new HashSet<Integer>().add(x))
// .collect(Collectors.toList());
// System.out.println(duplicateElementsParallel);
// Output: [1, 2, 3]
// find largest element of the list using streams.
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Integer largestElement = findLargestElement(list1);
System.out.println("Largest Element :: " + largestElement);
// find largest element of the list using streams by comparing to Integer
// argument.
Integer largestElementByComparing = findLargestElementByComparing(list1, 5);
System.out.println("Largest Element (by comparing 5) :: " + largestElementByComparing);
// Output: 9, 5
// Other stream operations
// Stream<Integer> stream = Arrays.asList(1, 2, 3, 4, 5).stream();
// stream.forEach(System.out::println); // Output: 1, 2, 3, 4, 5
// stream.filter(x -> x % 2 == 0).forEach(System.out::println); // Output: 2, 4
// stream.map(x -> x * 2).forEach(System.out::println); // Output: 2, 4, 6, 8,
// 10
// find smallest element of the list using streams.
List<Integer> list2 = Arrays.asList(-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
Integer smallestElement = findSmallestElement(list2);
System.out.println("Smallest Element :: " + smallestElement);
// Output: 1
// In a Given String, find the first non-repeated character using streams API.
String str = "hello";
char ch = str.chars()
.mapToObj(c -> (char) c)
.filter(c -> str.indexOf(c) == str.lastIndexOf(c))
.findFirst()
.orElse('\0');
System.out.println("First non-repeated character :: " + ch);
// In a given String, ���nd the ���rst non-repeated character using streams API
String s1 = "harini";
char ch1 = findFirstNonRepeatativeCharacter(s1);
System.out.println("First non-repeated character :: " + ch1);
}
}