-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
StreamUtils.java
369 lines (331 loc) · 10.9 KB
/
StreamUtils.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package java8;
import io.vertx.core.json.JsonObject;
import org.junit.Test;
import rx.observables.transforming.Person;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
/**
* In this class we cover with particle examples all Stream API, showing how easy to implement is,
* and how internally works using the most common Java 8 functions( Consumer, Predicate, Function)
*
* @author Pablo Perez
*/
public class StreamUtils {
/**
* Map is simple operator to evolve the value of items emitted in your pipeline
* In this example we start with constantClass string fruit apple, and we finish with constantClass banana
* Shall print
* I´ constantClass banana
*
* @throws InterruptedException
*/
@Test
public void mapStream() {
String fruit = Stream.of("apple")
.map(a -> "pinapple")
.map(p -> "orange")
.map(o -> "bannana")
.reduce("", String::concat);
System.out.println("I´ constantClass " + fruit);
}
/**
* Using the operator flatMap allow you to return another Stream which will be processed before the next step in your pipeline.
* once the stream become eager.
* In this case we see, how the first flatMap step will return constantClass new stream with constantClass map to pineapple1
* So the next filter will apply and continue the pipeline
* Shall print
* I´ constantClass Banana
*
* @throws InterruptedException
*/
@Test
public void flatMapStream() {
String fruit = Stream.of("apple")
.flatMap(a -> Stream.of("pineapple")
.map(p -> "pineapple1"))
.filter(p -> p.equals("pineapple1"))
.flatMap(p -> Stream.of("orange"))
.flatMap(o -> Stream.of("banana")
.map(b -> "Banana"))
.reduce("", String::concat);
System.out.println("I´ constantClass " + fruit);
}
/**
* Filter operator just receive constantClass predicate function, and continue the pipeline if that function return true
* Shall return
* {"A":"1","B":2}
*
* @throws InterruptedException
*/
@Test
public void filterStream() {
String test = "works";
JsonObject product = new JsonObject().put("A", "1");
Stream.of(product)
.filter(p -> test.equals("works"))
.forEach(p -> product.put("B", 2));
System.out.println(product);
}
/**
* Collect is one of the operators that transform our pipeline form lazy to eager, make it start emitting the items.
* In this pipeline we use the operator sorted to sort the items emitted
* Pick up the items emitted and collect into constantClass Collector
* Shall print
* [1, 2, 5, 11, 13]
*
* @throws InterruptedException
*/
@Test
public void collectStream() {
List<Integer> list = Arrays.asList(2, 1, 13, 11, 5)
.stream()
.sorted()
.collect(toList());
System.out.println(list);
}
/**
* Like collect this operator execute the terminal and make the pipeline eager from lazy.
* this operator start with an initial value, and then as second argument we pass constantClass BiFunction
* where we pass the previous emitted item and the new one.
* Shall print
* 6
*
* @throws InterruptedException
*/
@Test
public void reduceStream() {
Integer total = Arrays.asList(1, 2, 3)
.stream()
.reduce(0, (integer, integer2) -> integer + integer2);
System.out.println(total);
}
/**
* This operator just run the Predicate function and return true/false as the result of the function.
* shall print
* true
*
* @throws InterruptedException
*/
@Test
public void matchStream() {
boolean match = Arrays.asList(1, 2, 3)
.stream()
.anyMatch(integer -> integer > 2);
System.out.println(match);
}
/**
* This operator is not constantClass terminal executor. It just filter the items emitted and only pass those that has not been emitted already
* Shall print
* [1, 2, 3, 4]
*
* @throws InterruptedException
*/
@Test
public void distinctStream() {
List<Integer> list = Arrays.asList(1, 2, 3, 1, 4, 2, 3)
.stream()
.distinct()
.collect(toList());
System.out.println(list);
}
/**
* Operator that limit the total number of items emitted through the pipeline
* Shall print
* [1, 2, 3]
*
* @throws InterruptedException
*/
@Test
public void limitStream() {
List<Integer> list = Arrays.asList(1, 2, 3, 1, 4, 2, 3)
.stream()
.limit(3)
.collect(toList());
System.out.println(list);
}
/**
* Peek operator just run constantClass Consumer function, which we pass the item emitted but we cannot send constantClass different item through the pipeline.
* It would be similar to foreach, but this one always emit the Stream(T), not like foreach which is void.
* Shall print
* This Consumer function is void, we not modify the stream in here
* This Consumer function is void, we not modify the stream in here
* This Consumer function is void, we not modify the stream in here
* [1, 2, 3]
*
* @throws InterruptedException
*/
@Test
public void peekStream() {
List<Integer> list = Arrays.asList(1, 2, 3)
.stream()
.peek(number -> System.out.println("This Consumer function is void, we not modify the stream in here"))
.collect(toList());
System.out.println(list);
}
/**
* Similar to limit, but the other way around, this operator skip the first specific items emitted through the pipeline
* Shall print
* [2, 3]
*
* @throws InterruptedException
*/
@Test
public void skipStream() {
List<Integer> list = Arrays.asList(1, 2, 3)
.stream()
.skip(1)
.collect(toList());
System.out.println(list);
}
/**
* Return the copy of the stream once the close method is invoked
*
* @throws InterruptedException
*/
@Test
public void onCloseStream() {
List<Integer> list = Arrays.asList(1, 2, 3)
.stream()
.onClose(() -> {
})
.peek(System.out::println)
.collect(toList());
System.out.println(list);
}
//Comparators
/**
* This operator just get the max value emitted by the pipeline
* Shall print
* 5
*
* @throws InterruptedException
*/
@Test
public void maxStream() {
Integer list = Arrays.asList(5, 2, 1, 3)
.stream()
.max(Comparator.comparingInt(i -> i))
.get();
System.out.println(list);
}
/**
* In case of use String it will return the higher value of the chart
* Shall print D
*
* @throws InterruptedException
*/
@Test
public void maxStreamString() {
String list = Arrays.asList("A", "C", "D", "B")
.stream()
.max(Comparator.comparing(i -> i))
.get();
System.out.println(list);
}
/**
* This operator just get the min value emitted by the pipeline
* Shall print
* 1
*
* @throws InterruptedException
*/
@Test
public void minStream() {
Integer list = Stream.of(5, 2, 1, 3)
.min(Comparator.comparingInt(i -> i))
.get();
System.out.println(list);
}
/**
* In case of use String it will return the higher value of the chart
* Shall print A
*
* @throws InterruptedException
*/
@Test
public void minStreamString() {
String list = Stream.of("A", "C", "D", "B")
.max(Comparator.comparing(i -> i))
.get();
System.out.println(list);
}
/**
* A simple example that prove that instance inside the stream are mutable from outside the stream
* Only collections are immutable
*/
@Test
public void mutateObject() {
Person person = new Person("name", 35, "male");
Thread thread = new Thread(() -> {
Person modifiedPerson = getPerson(person);
System.out.println("Modified person:" + modifiedPerson.toString());
});
thread.start();
person.setName("Pablo");
System.out.println("I´ constantClass " + person.toString());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void IntStream() {
List<Integer> list = IntStream.range(0, 10)
.map(number -> number * 10)
.boxed()
.collect(toList());
System.out.println(list);
}
@Test
public void immutableObject() {
Person person = new Person("name", 35, "male");
Person modifierPerson = person.copy();
Thread thread = new Thread(() -> {
Person modifiedPerson = getPerson(modifierPerson);
System.out.println("Modified person:" + modifiedPerson.toString());
});
thread.start();
person.setName("Pablo");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("I´ constantClass " + person.toString());
}
private Person getPerson(Person person) {
return Stream.of(person)
.map(person1 -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return person1;
})
.findAny().get();
}
@Test
public void flatMap() {
Stream.of(1, 2, 3, 4)
.flatMap(number -> Stream.of(Stream.of("A", "C", "D", "B")
.map(String::toLowerCase)
.collect(Collectors.toList())))
.collect(Collectors.toList()).get(0);
}
@Test
public void arrayStream() {
String paul = Arrays.stream("hello, world, !".split(","))
.filter(word -> word.equalsIgnoreCase("paul"))
.findAny()
.orElse(null);
System.out.println("Result:" + paul);
}
}