-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathStreamsHowTheyWorksLesson.java
More file actions
60 lines (53 loc) · 2.37 KB
/
StreamsHowTheyWorksLesson.java
File metadata and controls
60 lines (53 loc) · 2.37 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
package java8;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class StreamsHowTheyWorksLesson {
public static void main(String[] args) {
//Processing Order
Stream<String> stream = Stream.of("a", "c", "b");
stream.map(s -> s.toUpperCase());
stream.map(s -> s.toUpperCase())
.filter(s -> s.equals("a"))
.forEach(s -> System.out.println(s));
stream.map((s) -> {
System.out.println("first map: " + s);
return s + 1;
}).map((s) -> {
System.out.println("second map: " + s);
return s.toUpperCase();})
.forEach(s -> System.out.println(s));
stream.sorted((s1, s2) -> {
System.out.println("sort s1: " + s1 + " s2: " + s2);
return s1.compareTo(s2);
}).map(s -> {
System.out.println("map: " + s);
return s.toUpperCase();
}).forEach(s -> System.out.println(s));
//Reusing Streams
Stream<String> stream2 = Stream.of("a", "b", "c");
Supplier<Stream<String>> supplier = () -> Stream.of("a", "b", "c");
stream2.forEach(s -> System.out.println(s));
stream2.forEach(s -> System.out.println(s));
//Parallel streams
ForkJoinPool commonPool = ForkJoinPool.commonPool();
System.out.println(commonPool.getParallelism());
Stream<String> stream3 = Stream.of("a", "b", "c");
stream3.parallel()
.map(s -> {
System.out.println("Map: " + s + " " + Thread.currentThread().getName() + " " + Thread.currentThread().getId());
return s.toUpperCase();
})
.map(s -> {
System.out.println("Map2: " + s + " " + Thread.currentThread().getName() + " " + Thread.currentThread().getId());
return s.toLowerCase();
})
.sorted((s1, s2) -> {
System.out.println("Sort: " + s1 + " " + s2 + " " + Thread.currentThread().getName() + " " + Thread.currentThread().getId());
return s1.compareTo(s2);
})
.forEach(s -> {
System.out.println("foreach: " + s + " " + Thread.currentThread().getName() + " " + Thread.currentThread().getId());
});
}
}