Skip to content

Commit 67de25a

Browse files
committed
Add samples
1 parent 0aac700 commit 67de25a

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/com/winterbe/java8/Streams11.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public static void main(String[] args) {
3535
// test2(persons);
3636
// test3(persons);
3737
// test4(persons);
38-
test5(persons);
38+
// test5(persons);
39+
test6(persons);
3940
}
4041

4142
private static void test1(List<Person> persons) {
@@ -97,4 +98,22 @@ private static void test5(List<Person> persons) {
9798

9899
System.out.println(ageSum);
99100
}
101+
102+
private static void test6(List<Person> persons) {
103+
Integer ageSum = persons
104+
.parallelStream()
105+
.reduce(0,
106+
(sum, p) -> {
107+
System.out.format("accumulator: sum=%s; person=%s; thread=%s\n",
108+
sum, p, Thread.currentThread().getName());
109+
return sum += p.age;
110+
},
111+
(sum1, sum2) -> {
112+
System.out.format("combiner: sum1=%s; sum2=%s; thread=%s\n",
113+
sum1, sum2, Thread.currentThread().getName());
114+
return sum1 + sum2;
115+
});
116+
117+
System.out.println(ageSum);
118+
}
100119
}

0 commit comments

Comments
 (0)