2525import static java .util .stream .Collectors .counting ;
2626import static java .util .stream .Collectors .groupingBy ;
2727import static java .util .stream .Collectors .joining ;
28- import static java .util .stream .Collectors .maxBy ;
28+ import static java .util .stream .Collectors .* ;
2929import static java .util .stream .Collectors .partitioningBy ;
3030import static java .util .stream .Collectors .summarizingDouble ;
3131import static java .util .stream .Collectors .summingDouble ;
3838
3939public class Java8CollectorsTest {
4040
41+ private final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dd" );
42+
4143 @ Test
4244 public void whenCollectingToList_shouldCollectToList () throws Exception {
43- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
44-
4545 final List <String > result = givenList .stream ()
4646 .collect (toList ());
4747
@@ -51,8 +51,6 @@ public void whenCollectingToList_shouldCollectToList() throws Exception {
5151
5252 @ Test
5353 public void whenCollectingToList_shouldCollectToSet () throws Exception {
54- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
55-
5654 final Set <String > result = givenList .stream ()
5755 .collect (toSet ());
5856
@@ -62,8 +60,6 @@ public void whenCollectingToList_shouldCollectToSet() throws Exception {
6260
6361 @ Test
6462 public void whenCollectingToCollection_shouldCollectToCollection () throws Exception {
65- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
66-
6763 final List <String > result = givenList .stream ()
6864 .collect (toCollection (LinkedList ::new ));
6965
@@ -74,8 +70,6 @@ public void whenCollectingToCollection_shouldCollectToCollection() throws Except
7470
7571 @ Test
7672 public void whenCollectingToImmutableCollection_shouldThrowException () throws Exception {
77- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
78-
7973 assertThatThrownBy (() -> {
8074 givenList .stream ()
8175 .collect (toCollection (ImmutableList ::of ));
@@ -85,36 +79,30 @@ public void whenCollectingToImmutableCollection_shouldThrowException() throws Ex
8579
8680 @ Test
8781 public void whenCollectingToMap_shouldCollectToMap () throws Exception {
88- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
89-
9082 final Map <String , Integer > result = givenList .stream ()
9183 .collect (toMap (Function .identity (), String ::length ));
9284
9385 assertThat (result )
9486 .containsEntry ("a" , 1 )
9587 .containsEntry ("bb" , 2 )
9688 .containsEntry ("ccc" , 3 )
97- .containsEntry ("dddd " , 4 );
89+ .containsEntry ("dd " , 2 );
9890 }
9991
10092 @ Test
10193 public void whenCollectingToMap_shouldCollectToMapMerging () throws Exception {
102- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
103-
10494 final Map <String , Integer > result = givenList .stream ()
10595 .collect (toMap (Function .identity (), String ::length , (i1 , i2 ) -> i1 ));
10696
10797 assertThat (result )
10898 .containsEntry ("a" , 1 )
10999 .containsEntry ("bb" , 2 )
110100 .containsEntry ("ccc" , 3 )
111- .containsEntry ("dddd " , 4 );
101+ .containsEntry ("dd " , 2 );
112102 }
113103
114104 @ Test
115105 public void whenCollectingAndThen_shouldCollect () throws Exception {
116- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
117-
118106 final List <String > result = givenList .stream ()
119107 .collect (collectingAndThen (toList (), ImmutableList ::copyOf ));
120108
@@ -125,59 +113,49 @@ public void whenCollectingAndThen_shouldCollect() throws Exception {
125113
126114 @ Test
127115 public void whenJoining_shouldJoin () throws Exception {
128- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
129-
130116 final String result = givenList .stream ()
131117 .collect (joining ());
132118
133119 assertThat (result )
134- .isEqualTo ("abbcccdddd " );
120+ .isEqualTo ("abbcccdd " );
135121 }
136122
137123 @ Test
138124 public void whenJoiningWithSeparator_shouldJoinWithSeparator () throws Exception {
139- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
140-
141125 final String result = givenList .stream ()
142126 .collect (joining (" " ));
143127
144128 assertThat (result )
145- .isEqualTo ("a bb ccc dddd " );
129+ .isEqualTo ("a bb ccc dd " );
146130 }
147131
148132 @ Test
149133 public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost () throws Exception {
150- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
151-
152134 final String result = givenList .stream ()
153135 .collect (joining (" " , "PRE-" , "-POST" ));
154136
155137 assertThat (result )
156- .isEqualTo ("PRE-a bb ccc dddd -POST" );
138+ .isEqualTo ("PRE-a bb ccc dd -POST" );
157139 }
158140
159141 @ Test
160142 public void whenPartitioningBy_shouldPartition () throws Exception {
161- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
162-
163143 final Map <Boolean , List <String >> result = givenList .stream ()
164144 .collect (partitioningBy (s -> s .length () > 2 ));
165145
166146 assertThat (result )
167147 .containsKeys (true , false )
168148 .satisfies (booleanListMap -> {
169149 assertThat (booleanListMap .get (true ))
170- .contains ("ccc" , "dddd" );
150+ .contains ("ccc" );
171151
172152 assertThat (booleanListMap .get (false ))
173- .contains ("a" , "bb" );
153+ .contains ("a" , "bb" , "dd" );
174154 });
175155 }
176156
177157 @ Test
178158 public void whenCounting_shouldCount () throws Exception {
179- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
180-
181159 final Long result = givenList .stream ()
182160 .collect (counting ());
183161
@@ -187,10 +165,8 @@ public void whenCounting_shouldCount() throws Exception {
187165
188166 @ Test
189167 public void whenSummarizing_shouldSummarize () throws Exception {
190- final List <Double > givenList = Arrays .asList (1d , 2d , 3d , 2d );
191-
192168 final DoubleSummaryStatistics result = givenList .stream ()
193- .collect (summarizingDouble (d -> d ));
169+ .collect (summarizingDouble (String :: length ));
194170
195171 assertThat (result .getAverage ())
196172 .isEqualTo (2 );
@@ -206,63 +182,52 @@ public void whenSummarizing_shouldSummarize() throws Exception {
206182
207183 @ Test
208184 public void whenAveraging_shouldAverage () throws Exception {
209- final List <Double > givenList = Arrays .asList (1d , 2d , 3d , 2d );
210-
211185 final Double result = givenList .stream ()
212- .collect (averagingDouble (d -> d ));
186+ .collect (averagingDouble (String :: length ));
213187
214188 assertThat (result )
215189 .isEqualTo (2 );
216190 }
217191
218192 @ Test
219193 public void whenSumming_shouldSum () throws Exception {
220- final List <Double > givenList = Arrays .asList (1d , 2d , 3d , 2d );
221-
222194 final Double result = givenList .stream ()
223- .collect (summingDouble (d -> d ));
195+ .collect (summingDouble (String :: length ));
224196
225197 assertThat (result )
226198 .isEqualTo (8 );
227199 }
228200
229201 @ Test
230202 public void whenMaxingBy_shouldMaxBy () throws Exception {
231- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
232-
233203 final Optional <String > result = givenList .stream ()
234204 .collect (maxBy (Comparator .naturalOrder ()));
235205
236206 assertThat (result )
237207 .isPresent ()
238- .hasValue ("dddd " );
208+ .hasValue ("dd " );
239209 }
240210
241211 @ Test
242212 public void whenGroupingBy_shouldGroupBy () throws Exception {
243- final List <String > givenList = Arrays .asList ("a" , "b" , "bb" , "ccc" , "dddd" );
244-
245213 final Map <Integer , Set <String >> result = givenList .stream ()
246214 .collect (groupingBy (String ::length , toSet ()));
247215
248216 assertThat (result )
249- .containsEntry (1 , newHashSet ("a" , "b" ))
250- .containsEntry (2 , newHashSet ("bb" ))
251- .containsEntry (3 , newHashSet ("ccc" ))
252- .containsEntry (4 , newHashSet ("dddd" ));
217+ .containsEntry (1 , newHashSet ("a" ))
218+ .containsEntry (2 , newHashSet ("bb" , "dd" ))
219+ .containsEntry (3 , newHashSet ("ccc" ));
253220 }
254221
255222
256223 @ Test
257224 public void whenCreatingCustomCollector_shouldCollect () throws Exception {
258- final List <String > givenList = Arrays .asList ("a" , "bb" , "ccc" , "dddd" );
259-
260225 final ImmutableSet <String > result = givenList .stream ()
261226 .collect (toImmutableSet ());
262227
263228 assertThat (result )
264229 .isInstanceOf (ImmutableSet .class )
265- .contains ("a" , "bb" , "ccc" , "dddd " );
230+ .contains ("a" , "bb" , "ccc" , "dd " );
266231
267232 }
268233
0 commit comments