Skip to content

Commit 3101b26

Browse files
committed
Add collector examples
1 parent 131d87d commit 3101b26

File tree

1 file changed

+140
-6
lines changed

1 file changed

+140
-6
lines changed

src/com/winterbe/java8/Streams10.java

Lines changed: 140 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.winterbe.java8;
22

33
import java.util.Arrays;
4+
import java.util.IntSummaryStatistics;
45
import java.util.List;
56
import java.util.Map;
7+
import java.util.StringJoiner;
8+
import java.util.stream.Collector;
69
import java.util.stream.Collectors;
710

811
/**
@@ -26,23 +29,154 @@ public String toString() {
2629
}
2730

2831
public static void main(String[] args) {
29-
test1();
30-
}
31-
32-
private static void test1() {
3332
List<Person> persons =
3433
Arrays.asList(
3534
new Person("Max", 18),
3635
new Person("Peter", 23),
37-
new Person("Brenda", 23),
36+
new Person("Pamela", 23),
3837
new Person("David", 12));
3938

39+
// test1(persons);
40+
// test2(persons);
41+
// test3(persons);
42+
// test4(persons);
43+
// test5(persons);
44+
// test6(persons);
45+
// test7(persons);
46+
// test8(persons);
47+
test9(persons);
48+
}
49+
50+
private static void test1(List<Person> persons) {
51+
List<Person> filtered =
52+
persons
53+
.stream()
54+
.filter(p -> p.name.startsWith("P"))
55+
.collect(Collectors.toList());
56+
57+
System.out.println(filtered); // [Peter, Pamela]
58+
}
59+
60+
private static void test2(List<Person> persons) {
4061
Map<Integer, List<Person>> personsByAge = persons
4162
.stream()
4263
.collect(Collectors.groupingBy(p -> p.age));
4364

4465
personsByAge
45-
.forEach((age, p) -> System.out.format("age: %s; persons: %s\n", age, p));
66+
.forEach((age, p) -> System.out.format("age %s: %s\n", age, p));
67+
68+
// age 18: [Max]
69+
// age 23:[Peter, Pamela]
70+
// age 12:[David]
71+
}
72+
73+
private static void test3(List<Person> persons) {
74+
Double averageAge = persons
75+
.stream()
76+
.collect(Collectors.averagingInt(p -> p.age));
77+
78+
System.out.println(averageAge); // 19.0
79+
}
80+
81+
private static void test4(List<Person> persons) {
82+
IntSummaryStatistics ageSummary =
83+
persons
84+
.stream()
85+
.collect(Collectors.summarizingInt(p -> p.age));
86+
87+
System.out.println(ageSummary);
88+
// IntSummaryStatistics{count=4, sum=76, min=12, average=19,000000, max=23}
89+
}
90+
91+
private static void test5(List<Person> persons) {
92+
String names = persons
93+
.stream()
94+
.filter(p -> p.age >= 18)
95+
.map(p -> p.name)
96+
.collect(Collectors.joining(" and ", "In Germany ", " are of legal age."));
97+
98+
System.out.println(names);
99+
// In Germany Max and Peter and Pamela are of legal age.
100+
}
101+
102+
private static void test6(List<Person> persons) {
103+
Map<Integer, String> map = persons
104+
.stream()
105+
.collect(Collectors.toMap(
106+
p -> p.age,
107+
p -> p.name,
108+
(name1, name2) -> name1 + ";" + name2));
109+
110+
System.out.println(map);
111+
// {18=Max, 23=Peter;Pamela, 12=David}
112+
}
113+
114+
private static void test7(List<Person> persons) {
115+
Collector<Person, StringJoiner, String> personNameCollector =
116+
Collector.of(
117+
() -> new StringJoiner(" | "), // supplier
118+
(j, p) -> j.add(p.name.toUpperCase()), // accumulator
119+
(j1, j2) -> j1.merge(j2), // combiner
120+
StringJoiner::toString); // finisher
121+
122+
String names = persons
123+
.stream()
124+
.collect(personNameCollector);
125+
126+
System.out.println(names); // MAX | PETER | PAMELA | DAVID
46127
}
47128

129+
private static void test8(List<Person> persons) {
130+
Collector<Person, StringJoiner, String> personNameCollector =
131+
Collector.of(
132+
() -> {
133+
System.out.println("supplier");
134+
return new StringJoiner(" | ");
135+
},
136+
(j, p) -> {
137+
System.out.format("accumulator: p=%s; j=%s\n", p, j);
138+
j.add(p.name.toUpperCase());
139+
},
140+
(j1, j2) -> {
141+
System.out.println("merge");
142+
return j1.merge(j2);
143+
},
144+
j -> {
145+
System.out.println("finisher");
146+
return j.toString();
147+
});
148+
149+
String names = persons
150+
.stream()
151+
.collect(personNameCollector);
152+
153+
System.out.println(names); // MAX | PETER | PAMELA | DAVID
154+
}
155+
156+
private static void test9(List<Person> persons) {
157+
Collector<Person, StringJoiner, String> personNameCollector =
158+
Collector.of(
159+
() -> {
160+
System.out.println("supplier");
161+
return new StringJoiner(" | ");
162+
},
163+
(j, p) -> {
164+
System.out.format("accumulator: p=%s; j=%s\n", p, j);
165+
j.add(p.name.toUpperCase());
166+
},
167+
(j1, j2) -> {
168+
System.out.println("merge");
169+
return j1.merge(j2);
170+
},
171+
j -> {
172+
System.out.println("finisher");
173+
return j.toString();
174+
});
175+
176+
String names = persons
177+
.parallelStream()
178+
.collect(personNameCollector);
179+
180+
System.out.println(names); // MAX | PETER | PAMELA | DAVID
181+
}
48182
}

0 commit comments

Comments
 (0)