forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreams12.java
More file actions
57 lines (49 loc) · 1.83 KB
/
Streams12.java
File metadata and controls
57 lines (49 loc) · 1.83 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
package com.winterbe.java8;
import java.util.Arrays;
import java.util.List;
/**
* @author Benjamin Winterberg
*/
public class Streams12 {
public static void main(String[] args) {
List<String> strings = Arrays.asList("a1", "a2", "b1", "c2", "c1");
// test1(strings);
// test2(strings);
test3(strings);
}
private static void test3(List<String> strings) {
strings
.parallelStream()
.filter(s -> {
System.out.format("filter: %s [%s]\n", s, Thread.currentThread().getName());
return true;
})
.map(s -> {
System.out.format("map: %s [%s]\n", s, Thread.currentThread().getName());
return s.toUpperCase();
})
.sorted((s1, s2) -> {
System.out.format("sort: %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName());
return s1.compareTo(s2);
})
.forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));
}
private static void test2(List<String> strings) {
strings
.parallelStream()
.filter(s -> {
System.out.format("filter: %s [%s]\n", s, Thread.currentThread().getName());
return true;
})
.map(s -> {
System.out.format("map: %s [%s]\n", s, Thread.currentThread().getName());
return s.toUpperCase();
})
.forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));
}
private static void test1() {
// -Djava.util.concurrent.ForkJoinPool.common.parallelism=5
// ForkJoinPool commonPool = ForkJoinPool.commonPool();
// System.out.println(commonPool.getParallelism());
}
}