-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamJava8Part2
138 lines (128 loc) · 6.23 KB
/
StreamJava8Part2
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
126
127
128
129
130
131
132
133
134
135
136
137
138
//Stream Operations Part 2
import java.util.stream.*;
import java.util.*;
import java.util.Comparator;
public class MyClass {
public static void main(String args[]) {
createStreamAndGetFirstElement();
createStreamAndGetLastElement();
duplicateInStream();
countOccurenceOfChar('?');
getArrayListFromStream();
}
//Create a stream of string and find the first element in the stream
public static void createStreamAndGetFirstElement(){
//-----------------------------using reduce----------------------------------------------
//if we do not use orElse, it returns an Optional object not string
Stream<String> s=Stream.of("Hey","Hello","Hi","Hola");
System.out.println(s.reduce((first,second)->first).orElse(null));
/*Output:
Hey
*/
//----------------------------------------------------------------------------------------
//----------------------------using findFirst---------------------------------------------
s=Stream.of("Hey","Hello","Hi","Hola");
System.out.println(s.findFirst().orElse(null));
/*Output:
Hey
*/
//----------------------------------------------------------------------------------------
//---------------checking same operation on null/empty stream-----------------------------
//checking same operation on null/empty stream
s=Stream.empty();
System.out.println(s.findFirst().orElse(null));
/*Output:
null
*/
//----------------------------------------------------------------------------------------
}
//Create a stream of string and find the last element in the stream
public static void createStreamAndGetLastElement(){
//--------------------------------using reduce--------------------------------------------
Stream<String> s=Stream.of("Hey","Hello","Hi","Hola");
System.out.println(s.reduce((first,second)->second).orElse(null));
/*Output:
Hola
*/
//----------------------------------------------------------------------------------------
//--------------------------------using skip----------------------------------------------
s=Stream.of("Hey","Hello","Hi","Hola");
int N=4;
System.out.println(s.skip(N-1).findFirst().orElse(null));
/*Output:
Hola
*/
//----------------------------------------------------------------------------------------
//---------------------using sorted reverse order------------------------------------------
s=Stream.of("Hey","Hello","Hi","Hola");
System.out.println(s.sorted(Comparator.reverseOrder()).findFirst().orElse(null));
/*Output:
Hola
*/
//----------------------------------------------------------------------------------------
}
//find duplicate elements in a stream
public static void duplicateInStream(){
//---------------------using Set.add()---------------------------------------------------
//add returns false if element already present
Stream<Integer> s=Stream.of(1,2,3,4,2,5,6,1,3,3);
Set<Integer> set=new HashSet<>();
s.filter(i->!set.add(i)).collect(Collectors.toSet()).forEach(System.out::println);
/*Output:
1
2
3
*/
//----------------------------------------------------------------------------------------
//---------------------using Collections.frequency()--------------------------------------
//works with collection only
//To understand how frequency() works, visit https://www.geeksforgeeks.org/java-util-collections-frequency-java/
List<Integer> list = Arrays.asList(1,2,3,4,2,5,6,1,3,3);
list.stream().filter(i->Collections.frequency(list,i)>1).collect(Collectors.toSet()).forEach(System.out::println);
/*Output:
1
2
3
*/
//----------------------------------------------------------------------------------------
}
//Count occurence of a given character in a given string
public static void countOccurenceOfChar(char toFind){
//-----------------------------using chars()----------------------------------------------
String s="Hey Siri, how you doing?";
System.out.println("Occurences of Character "+toFind+" -> "+s.chars().filter(i -> i==toFind).count());
/*Output:
Occurences of Character ? -> 1
*/
//----------------------------------------------------------------------------------------
}
//Get arrayList from Stream
public static void getArrayListFromStream(){
//----------------------Collectors.toList()-----------------------------------------------
Stream<Integer> s=Stream.of(1,2,3,4,2,5,6,1,3,3);
s.collect(Collectors.toList()).forEach(System.out::print);
//----------------------------------------------------------------------------------------
/*Output:
1234256133
*/
//----------------------------------------------------------------------------------------
//----------------------Collectors.toCollection()----------------------------------------
//a more generic way for collections overall
s=Stream.of(1,2,3,4,2,5,6,1,3,3);
s.collect(Collectors.toCollection(ArrayList::new)).forEach(System.out::print);
//----------------------------------------------------------------------------------------
/*Output:
1234256133
*/
//----------------------------------------------------------------------------------------
//----------------------Collectors.toCollection()-----------------------------------------
//making set instead of list
s=Stream.of(1,2,3,4,2,5,6,1,3,3);
s.collect(Collectors.toCollection(HashSet::new)).forEach(System.out::print);
//----------------------------------------------------------------------------------------
/*Output:
123456
*/
//-----------------------------------------------------------------------------------------
}
}