@@ -42,96 +42,136 @@ public class Java8CollectorsUnitTest {
42
42
43
43
@ Test
44
44
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 ());
46
48
47
49
assertThat (result ).containsAll (givenList );
48
50
}
49
51
50
52
@ Test
51
53
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 ());
53
57
54
58
assertThat (result ).containsAll (givenList );
55
59
}
56
60
57
61
@ Test
58
62
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 ));
60
66
61
- assertThat (result ).containsAll (givenList ).isInstanceOf (LinkedList .class );
67
+ assertThat (result )
68
+ .containsAll (givenList )
69
+ .isInstanceOf (LinkedList .class );
62
70
}
63
71
64
72
@ Test
65
73
public void whenCollectingToImmutableCollection_shouldThrowException () throws Exception {
66
74
assertThatThrownBy (() -> {
67
- givenList .stream ().collect (toCollection (ImmutableList ::of ));
75
+ givenList
76
+ .stream ()
77
+ .collect (toCollection (ImmutableList ::of ));
68
78
}).isInstanceOf (UnsupportedOperationException .class );
69
79
70
80
}
71
81
72
82
@ Test
73
83
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 );
77
93
}
78
94
79
95
@ Test
80
96
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 );
84
106
}
85
107
86
108
@ Test
87
109
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 ));
89
113
90
- assertThat (result ).containsAll (givenList ).isInstanceOf (ImmutableList .class );
114
+ assertThat (result )
115
+ .containsAll (givenList )
116
+ .isInstanceOf (ImmutableList .class );
91
117
}
92
118
93
119
@ Test
94
120
public void whenJoining_shouldJoin () throws Exception {
95
- final String result = givenList .stream ().collect (joining ());
121
+ final String result = givenList
122
+ .stream ()
123
+ .collect (joining ());
96
124
97
125
assertThat (result ).isEqualTo ("abbcccdd" );
98
126
}
99
127
100
128
@ Test
101
129
public void whenJoiningWithSeparator_shouldJoinWithSeparator () throws Exception {
102
- final String result = givenList .stream ().collect (joining (" " ));
130
+ final String result = givenList
131
+ .stream ()
132
+ .collect (joining (" " ));
103
133
104
134
assertThat (result ).isEqualTo ("a bb ccc dd" );
105
135
}
106
136
107
137
@ Test
108
138
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" ));
110
142
111
143
assertThat (result ).isEqualTo ("PRE-a bb ccc dd-POST" );
112
144
}
113
145
114
146
@ Test
115
147
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 ));
117
151
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" );
120
156
121
- assertThat (booleanListMap .get (false )).contains ("a" , "bb" , "dd" );
122
- });
157
+ assertThat (booleanListMap .get (false )).contains ("a" , "bb" , "dd" );
158
+ });
123
159
}
124
160
125
161
@ Test
126
162
public void whenCounting_shouldCount () throws Exception {
127
- final Long result = givenList .stream ().collect (counting ());
163
+ final Long result = givenList
164
+ .stream ()
165
+ .collect (counting ());
128
166
129
167
assertThat (result ).isEqualTo (4 );
130
168
}
131
169
132
170
@ Test
133
171
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 ));
135
175
136
176
assertThat (result .getAverage ()).isEqualTo (2 );
137
177
assertThat (result .getCount ()).isEqualTo (4 );
@@ -142,37 +182,55 @@ public void whenSummarizing_shouldSummarize() throws Exception {
142
182
143
183
@ Test
144
184
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 ));
146
188
147
189
assertThat (result ).isEqualTo (2 );
148
190
}
149
191
150
192
@ Test
151
193
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 ));
153
198
154
199
assertThat (result ).isEqualTo (8 );
155
200
}
156
201
157
202
@ Test
158
203
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 ()));
160
207
161
- assertThat (result ).isPresent ().hasValue ("dd" );
208
+ assertThat (result )
209
+ .isPresent ()
210
+ .hasValue ("dd" );
162
211
}
163
212
164
213
@ Test
165
214
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" ));
169
223
}
170
224
171
225
@ Test
172
226
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 ());
174
230
175
- assertThat (result ).isInstanceOf (ImmutableSet .class ).contains ("a" , "bb" , "ccc" , "dd" );
231
+ assertThat (result )
232
+ .isInstanceOf (ImmutableSet .class )
233
+ .contains ("a" , "bb" , "ccc" , "dd" );
176
234
177
235
}
178
236
0 commit comments