@@ -42,96 +42,136 @@ public class Java8CollectorsUnitTest {
4242
4343 @ Test
4444 public void whenCollectingToList_shouldCollectToList () throws Exception {
45- final List <String > result = givenList .stream ().collect (toList ());
45+ final List <String > result = givenList
46+ .stream ()
47+ .collect (toList ());
4648
4749 assertThat (result ).containsAll (givenList );
4850 }
4951
5052 @ Test
5153 public void whenCollectingToList_shouldCollectToSet () throws Exception {
52- final Set <String > result = givenList .stream ().collect (toSet ());
54+ final Set <String > result = givenList
55+ .stream ()
56+ .collect (toSet ());
5357
5458 assertThat (result ).containsAll (givenList );
5559 }
5660
5761 @ Test
5862 public void whenCollectingToCollection_shouldCollectToCollection () throws Exception {
59- final List <String > result = givenList .stream ().collect (toCollection (LinkedList ::new ));
63+ final List <String > result = givenList
64+ .stream ()
65+ .collect (toCollection (LinkedList ::new ));
6066
61- assertThat (result ).containsAll (givenList ).isInstanceOf (LinkedList .class );
67+ assertThat (result )
68+ .containsAll (givenList )
69+ .isInstanceOf (LinkedList .class );
6270 }
6371
6472 @ Test
6573 public void whenCollectingToImmutableCollection_shouldThrowException () throws Exception {
6674 assertThatThrownBy (() -> {
67- givenList .stream ().collect (toCollection (ImmutableList ::of ));
75+ givenList
76+ .stream ()
77+ .collect (toCollection (ImmutableList ::of ));
6878 }).isInstanceOf (UnsupportedOperationException .class );
6979
7080 }
7181
7282 @ Test
7383 public void whenCollectingToMap_shouldCollectToMap () throws Exception {
74- final Map <String , Integer > result = givenList .stream ().collect (toMap (Function .identity (), String ::length ));
75-
76- assertThat (result ).containsEntry ("a" , 1 ).containsEntry ("bb" , 2 ).containsEntry ("ccc" , 3 ).containsEntry ("dd" , 2 );
84+ final Map <String , Integer > result = givenList
85+ .stream ()
86+ .collect (toMap (Function .identity (), String ::length ));
87+
88+ assertThat (result )
89+ .containsEntry ("a" , 1 )
90+ .containsEntry ("bb" , 2 )
91+ .containsEntry ("ccc" , 3 )
92+ .containsEntry ("dd" , 2 );
7793 }
7894
7995 @ Test
8096 public void whenCollectingToMap_shouldCollectToMapMerging () throws Exception {
81- final Map <String , Integer > result = givenList .stream ().collect (toMap (Function .identity (), String ::length , (i1 , i2 ) -> i1 ));
82-
83- assertThat (result ).containsEntry ("a" , 1 ).containsEntry ("bb" , 2 ).containsEntry ("ccc" , 3 ).containsEntry ("dd" , 2 );
97+ final Map <String , Integer > result = givenList
98+ .stream ()
99+ .collect (toMap (Function .identity (), String ::length , (i1 , i2 ) -> i1 ));
100+
101+ assertThat (result )
102+ .containsEntry ("a" , 1 )
103+ .containsEntry ("bb" , 2 )
104+ .containsEntry ("ccc" , 3 )
105+ .containsEntry ("dd" , 2 );
84106 }
85107
86108 @ Test
87109 public void whenCollectingAndThen_shouldCollect () throws Exception {
88- final List <String > result = givenList .stream ().collect (collectingAndThen (toList (), ImmutableList ::copyOf ));
110+ final List <String > result = givenList
111+ .stream ()
112+ .collect (collectingAndThen (toList (), ImmutableList ::copyOf ));
89113
90- assertThat (result ).containsAll (givenList ).isInstanceOf (ImmutableList .class );
114+ assertThat (result )
115+ .containsAll (givenList )
116+ .isInstanceOf (ImmutableList .class );
91117 }
92118
93119 @ Test
94120 public void whenJoining_shouldJoin () throws Exception {
95- final String result = givenList .stream ().collect (joining ());
121+ final String result = givenList
122+ .stream ()
123+ .collect (joining ());
96124
97125 assertThat (result ).isEqualTo ("abbcccdd" );
98126 }
99127
100128 @ Test
101129 public void whenJoiningWithSeparator_shouldJoinWithSeparator () throws Exception {
102- final String result = givenList .stream ().collect (joining (" " ));
130+ final String result = givenList
131+ .stream ()
132+ .collect (joining (" " ));
103133
104134 assertThat (result ).isEqualTo ("a bb ccc dd" );
105135 }
106136
107137 @ Test
108138 public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost () throws Exception {
109- final String result = givenList .stream ().collect (joining (" " , "PRE-" , "-POST" ));
139+ final String result = givenList
140+ .stream ()
141+ .collect (joining (" " , "PRE-" , "-POST" ));
110142
111143 assertThat (result ).isEqualTo ("PRE-a bb ccc dd-POST" );
112144 }
113145
114146 @ Test
115147 public void whenPartitioningBy_shouldPartition () throws Exception {
116- final Map <Boolean , List <String >> result = givenList .stream ().collect (partitioningBy (s -> s .length () > 2 ));
148+ final Map <Boolean , List <String >> result = givenList
149+ .stream ()
150+ .collect (partitioningBy (s -> s .length () > 2 ));
117151
118- assertThat (result ).containsKeys (true , false ).satisfies (booleanListMap -> {
119- assertThat (booleanListMap .get (true )).contains ("ccc" );
152+ assertThat (result )
153+ .containsKeys (true , false )
154+ .satisfies (booleanListMap -> {
155+ assertThat (booleanListMap .get (true )).contains ("ccc" );
120156
121- assertThat (booleanListMap .get (false )).contains ("a" , "bb" , "dd" );
122- });
157+ assertThat (booleanListMap .get (false )).contains ("a" , "bb" , "dd" );
158+ });
123159 }
124160
125161 @ Test
126162 public void whenCounting_shouldCount () throws Exception {
127- final Long result = givenList .stream ().collect (counting ());
163+ final Long result = givenList
164+ .stream ()
165+ .collect (counting ());
128166
129167 assertThat (result ).isEqualTo (4 );
130168 }
131169
132170 @ Test
133171 public void whenSummarizing_shouldSummarize () throws Exception {
134- final DoubleSummaryStatistics result = givenList .stream ().collect (summarizingDouble (String ::length ));
172+ final DoubleSummaryStatistics result = givenList
173+ .stream ()
174+ .collect (summarizingDouble (String ::length ));
135175
136176 assertThat (result .getAverage ()).isEqualTo (2 );
137177 assertThat (result .getCount ()).isEqualTo (4 );
@@ -142,37 +182,55 @@ public void whenSummarizing_shouldSummarize() throws Exception {
142182
143183 @ Test
144184 public void whenAveraging_shouldAverage () throws Exception {
145- final Double result = givenList .stream ().collect (averagingDouble (String ::length ));
185+ final Double result = givenList
186+ .stream ()
187+ .collect (averagingDouble (String ::length ));
146188
147189 assertThat (result ).isEqualTo (2 );
148190 }
149191
150192 @ Test
151193 public void whenSumming_shouldSum () throws Exception {
152- final Double result = givenList .stream ().collect (summingDouble (String ::length ));
194+ final Double result = givenList
195+ .stream ()
196+ .filter (i -> true )
197+ .collect (summingDouble (String ::length ));
153198
154199 assertThat (result ).isEqualTo (8 );
155200 }
156201
157202 @ Test
158203 public void whenMaxingBy_shouldMaxBy () throws Exception {
159- final Optional <String > result = givenList .stream ().collect (maxBy (Comparator .naturalOrder ()));
204+ final Optional <String > result = givenList
205+ .stream ()
206+ .collect (maxBy (Comparator .naturalOrder ()));
160207
161- assertThat (result ).isPresent ().hasValue ("dd" );
208+ assertThat (result )
209+ .isPresent ()
210+ .hasValue ("dd" );
162211 }
163212
164213 @ Test
165214 public void whenGroupingBy_shouldGroupBy () throws Exception {
166- final Map <Integer , Set <String >> result = givenList .stream ().collect (groupingBy (String ::length , toSet ()));
167-
168- assertThat (result ).containsEntry (1 , newHashSet ("a" )).containsEntry (2 , newHashSet ("bb" , "dd" )).containsEntry (3 , newHashSet ("ccc" ));
215+ final Map <Integer , Set <String >> result = givenList
216+ .stream ()
217+ .collect (groupingBy (String ::length , toSet ()));
218+
219+ assertThat (result )
220+ .containsEntry (1 , newHashSet ("a" ))
221+ .containsEntry (2 , newHashSet ("bb" , "dd" ))
222+ .containsEntry (3 , newHashSet ("ccc" ));
169223 }
170224
171225 @ Test
172226 public void whenCreatingCustomCollector_shouldCollect () throws Exception {
173- final ImmutableSet <String > result = givenList .stream ().collect (toImmutableSet ());
227+ final ImmutableSet <String > result = givenList
228+ .stream ()
229+ .collect (toImmutableSet ());
174230
175- assertThat (result ).isInstanceOf (ImmutableSet .class ).contains ("a" , "bb" , "ccc" , "dd" );
231+ assertThat (result )
232+ .isInstanceOf (ImmutableSet .class )
233+ .contains ("a" , "bb" , "ccc" , "dd" );
176234
177235 }
178236
0 commit comments