11package com .baeldung .java8 ;
22
3- import static java .util .Comparator .comparingInt ;
4- import static java .util .stream .Collectors .averagingInt ;
5- import static java .util .stream .Collectors .counting ;
6- import static java .util .stream .Collectors .groupingBy ;
7- import static java .util .stream .Collectors .groupingByConcurrent ;
8- import static java .util .stream .Collectors .joining ;
9- import static java .util .stream .Collectors .mapping ;
10- import static java .util .stream .Collectors .maxBy ;
11- import static java .util .stream .Collectors .summarizingInt ;
12- import static java .util .stream .Collectors .summingInt ;
13- import static java .util .stream .Collectors .toList ;
14- import static java .util .stream .Collectors .toSet ;
15- import static org .junit .Assert .assertEquals ;
16- import static org .junit .Assert .assertNull ;
17- import static org .junit .Assert .assertTrue ;
18-
19- import java .util .Arrays ;
20- import java .util .EnumMap ;
21- import java .util .IntSummaryStatistics ;
22- import java .util .List ;
23- import java .util .Map ;
24- import java .util .Optional ;
25- import java .util .Set ;
26- import java .util .concurrent .ConcurrentMap ;
27-
28- import org .junit .Test ;
29-
303import com .baeldung .java_8_features .groupingby .BlogPost ;
314import com .baeldung .java_8_features .groupingby .BlogPostType ;
5+ import org .junit .Test ;
6+
7+ import java .util .*;
8+ import java .util .concurrent .ConcurrentMap ;
9+
10+ import static java .util .Comparator .comparingInt ;
11+ import static java .util .stream .Collectors .*;
12+ import static org .junit .Assert .*;
3213
3314public class Java8GroupingByCollectorUnitTest {
3415
35- private static final List <BlogPost > POSTS = Arrays .asList (new BlogPost ("News item 1" , "Author 1" , BlogPostType .NEWS , 15 ), new BlogPost ("Tech review 1" , "Author 2" , BlogPostType .REVIEW , 5 ),
36- new BlogPost ("Programming guide" , "Author 1" , BlogPostType .GUIDE , 20 ), new BlogPost ("News item 2" , "Author 2" , BlogPostType .NEWS , 35 ), new BlogPost ("Tech review 2" , "Author 1" , BlogPostType .REVIEW , 15 ));
16+ private static final List <BlogPost > posts = Arrays .asList (
17+ new BlogPost ("News item 1" , "Author 1" , BlogPostType .NEWS , 15 ),
18+ new BlogPost ("Tech review 1" , "Author 2" , BlogPostType .REVIEW , 5 ),
19+ new BlogPost ("Programming guide" , "Author 1" , BlogPostType .GUIDE , 20 ),
20+ new BlogPost ("News item 2" , "Author 2" , BlogPostType .NEWS , 35 ),
21+ new BlogPost ("Tech review 2" , "Author 1" , BlogPostType .REVIEW , 15 ));
3722
3823 @ Test
3924 public void givenAListOfPosts_whenGroupedByType_thenGetAMapBetweenTypeAndPosts () {
40- Map <BlogPostType , List <BlogPost >> postsPerType = POSTS .stream ().collect (groupingBy (BlogPost ::getType ));
41-
42- assertEquals (2 , postsPerType .get (BlogPostType .NEWS ).size ());
43- assertEquals (1 , postsPerType .get (BlogPostType .GUIDE ).size ());
44- assertEquals (2 , postsPerType .get (BlogPostType .REVIEW ).size ());
25+ Map <BlogPostType , List <BlogPost >> postsPerType = posts
26+ .stream ()
27+ .collect (groupingBy (BlogPost ::getType ));
28+
29+ assertEquals (2 , postsPerType
30+ .get (BlogPostType .NEWS )
31+ .size ());
32+ assertEquals (1 , postsPerType
33+ .get (BlogPostType .GUIDE )
34+ .size ());
35+ assertEquals (2 , postsPerType
36+ .get (BlogPostType .REVIEW )
37+ .size ());
4538 }
4639
4740 @ Test
4841 public void givenAListOfPosts_whenGroupedByTypeAndTheirTitlesAreJoinedInAString_thenGetAMapBetweenTypeAndCsvTitles () {
49- Map <BlogPostType , String > postsPerType = POSTS .stream ().collect (groupingBy (BlogPost ::getType , mapping (BlogPost ::getTitle , joining (", " , "Post titles: [" , "]" ))));
42+ Map <BlogPostType , String > postsPerType = posts
43+ .stream ()
44+ .collect (groupingBy (BlogPost ::getType , mapping (BlogPost ::getTitle , joining (", " , "Post titles: [" , "]" ))));
5045
5146 assertEquals ("Post titles: [News item 1, News item 2]" , postsPerType .get (BlogPostType .NEWS ));
5247 assertEquals ("Post titles: [Programming guide]" , postsPerType .get (BlogPostType .GUIDE ));
@@ -55,88 +50,174 @@ public void givenAListOfPosts_whenGroupedByTypeAndTheirTitlesAreJoinedInAString_
5550
5651 @ Test
5752 public void givenAListOfPosts_whenGroupedByTypeAndSumTheLikes_thenGetAMapBetweenTypeAndPostLikes () {
58- Map <BlogPostType , Integer > likesPerType = POSTS .stream ().collect (groupingBy (BlogPost ::getType , summingInt (BlogPost ::getLikes )));
59-
60- assertEquals (50 , likesPerType .get (BlogPostType .NEWS ).intValue ());
61- assertEquals (20 , likesPerType .get (BlogPostType .REVIEW ).intValue ());
62- assertEquals (20 , likesPerType .get (BlogPostType .GUIDE ).intValue ());
53+ Map <BlogPostType , Integer > likesPerType = posts
54+ .stream ()
55+ .collect (groupingBy (BlogPost ::getType , summingInt (BlogPost ::getLikes )));
56+
57+ assertEquals (50 , likesPerType
58+ .get (BlogPostType .NEWS )
59+ .intValue ());
60+ assertEquals (20 , likesPerType
61+ .get (BlogPostType .REVIEW )
62+ .intValue ());
63+ assertEquals (20 , likesPerType
64+ .get (BlogPostType .GUIDE )
65+ .intValue ());
6366 }
6467
6568 @ Test
6669 public void givenAListOfPosts_whenGroupedByTypeInAnEnumMap_thenGetAnEnumMapBetweenTypeAndPosts () {
67- EnumMap <BlogPostType , List <BlogPost >> postsPerType = POSTS .stream ().collect (groupingBy (BlogPost ::getType , () -> new EnumMap <>(BlogPostType .class ), toList ()));
68-
69- assertEquals (2 , postsPerType .get (BlogPostType .NEWS ).size ());
70- assertEquals (1 , postsPerType .get (BlogPostType .GUIDE ).size ());
71- assertEquals (2 , postsPerType .get (BlogPostType .REVIEW ).size ());
70+ EnumMap <BlogPostType , List <BlogPost >> postsPerType = posts
71+ .stream ()
72+ .collect (groupingBy (BlogPost ::getType , () -> new EnumMap <>(BlogPostType .class ), toList ()));
73+
74+ assertEquals (2 , postsPerType
75+ .get (BlogPostType .NEWS )
76+ .size ());
77+ assertEquals (1 , postsPerType
78+ .get (BlogPostType .GUIDE )
79+ .size ());
80+ assertEquals (2 , postsPerType
81+ .get (BlogPostType .REVIEW )
82+ .size ());
7283 }
7384
7485 @ Test
7586 public void givenAListOfPosts_whenGroupedByTypeInSets_thenGetAMapBetweenTypesAndSetsOfPosts () {
76- Map <BlogPostType , Set <BlogPost >> postsPerType = POSTS .stream ().collect (groupingBy (BlogPost ::getType , toSet ()));
77-
78- assertEquals (2 , postsPerType .get (BlogPostType .NEWS ).size ());
79- assertEquals (1 , postsPerType .get (BlogPostType .GUIDE ).size ());
80- assertEquals (2 , postsPerType .get (BlogPostType .REVIEW ).size ());
87+ Map <BlogPostType , Set <BlogPost >> postsPerType = posts
88+ .stream ()
89+ .collect (groupingBy (BlogPost ::getType , toSet ()));
90+
91+ assertEquals (2 , postsPerType
92+ .get (BlogPostType .NEWS )
93+ .size ());
94+ assertEquals (1 , postsPerType
95+ .get (BlogPostType .GUIDE )
96+ .size ());
97+ assertEquals (2 , postsPerType
98+ .get (BlogPostType .REVIEW )
99+ .size ());
81100 }
82101
83102 @ Test
84103 public void givenAListOfPosts_whenGroupedByTypeConcurrently_thenGetAMapBetweenTypeAndPosts () {
85- ConcurrentMap <BlogPostType , List <BlogPost >> postsPerType = POSTS .parallelStream ().collect (groupingByConcurrent (BlogPost ::getType ));
86-
87- assertEquals (2 , postsPerType .get (BlogPostType .NEWS ).size ());
88- assertEquals (1 , postsPerType .get (BlogPostType .GUIDE ).size ());
89- assertEquals (2 , postsPerType .get (BlogPostType .REVIEW ).size ());
104+ ConcurrentMap <BlogPostType , List <BlogPost >> postsPerType = posts
105+ .parallelStream ()
106+ .collect (groupingByConcurrent (BlogPost ::getType ));
107+
108+ assertEquals (2 , postsPerType
109+ .get (BlogPostType .NEWS )
110+ .size ());
111+ assertEquals (1 , postsPerType
112+ .get (BlogPostType .GUIDE )
113+ .size ());
114+ assertEquals (2 , postsPerType
115+ .get (BlogPostType .REVIEW )
116+ .size ());
90117 }
91118
92119 @ Test
93120 public void givenAListOfPosts_whenGroupedByTypeAndAveragingLikes_thenGetAMapBetweenTypeAndAverageNumberOfLikes () {
94- Map <BlogPostType , Double > averageLikesPerType = POSTS .stream ().collect (groupingBy (BlogPost ::getType , averagingInt (BlogPost ::getLikes )));
95-
96- assertEquals (25 , averageLikesPerType .get (BlogPostType .NEWS ).intValue ());
97- assertEquals (20 , averageLikesPerType .get (BlogPostType .GUIDE ).intValue ());
98- assertEquals (10 , averageLikesPerType .get (BlogPostType .REVIEW ).intValue ());
121+ Map <BlogPostType , Double > averageLikesPerType = posts
122+ .stream ()
123+ .collect (groupingBy (BlogPost ::getType , averagingInt (BlogPost ::getLikes )));
124+
125+ assertEquals (25 , averageLikesPerType
126+ .get (BlogPostType .NEWS )
127+ .intValue ());
128+ assertEquals (20 , averageLikesPerType
129+ .get (BlogPostType .GUIDE )
130+ .intValue ());
131+ assertEquals (10 , averageLikesPerType
132+ .get (BlogPostType .REVIEW )
133+ .intValue ());
99134 }
100135
101136 @ Test
102137 public void givenAListOfPosts_whenGroupedByTypeAndCounted_thenGetAMapBetweenTypeAndNumberOfPosts () {
103- Map <BlogPostType , Long > numberOfPostsPerType = POSTS .stream ().collect (groupingBy (BlogPost ::getType , counting ()));
104-
105- assertEquals (2 , numberOfPostsPerType .get (BlogPostType .NEWS ).intValue ());
106- assertEquals (1 , numberOfPostsPerType .get (BlogPostType .GUIDE ).intValue ());
107- assertEquals (2 , numberOfPostsPerType .get (BlogPostType .REVIEW ).intValue ());
138+ Map <BlogPostType , Long > numberOfPostsPerType = posts
139+ .stream ()
140+ .collect (groupingBy (BlogPost ::getType , counting ()));
141+
142+ assertEquals (2 , numberOfPostsPerType
143+ .get (BlogPostType .NEWS )
144+ .intValue ());
145+ assertEquals (1 , numberOfPostsPerType
146+ .get (BlogPostType .GUIDE )
147+ .intValue ());
148+ assertEquals (2 , numberOfPostsPerType
149+ .get (BlogPostType .REVIEW )
150+ .intValue ());
108151 }
109152
110153 @ Test
111154 public void givenAListOfPosts_whenGroupedByTypeAndMaxingLikes_thenGetAMapBetweenTypeAndMaximumNumberOfLikes () {
112- Map <BlogPostType , Optional <BlogPost >> maxLikesPerPostType = POSTS .stream ().collect (groupingBy (BlogPost ::getType , maxBy (comparingInt (BlogPost ::getLikes ))));
113-
114- assertTrue (maxLikesPerPostType .get (BlogPostType .NEWS ).isPresent ());
115- assertEquals (35 , maxLikesPerPostType .get (BlogPostType .NEWS ).get ().getLikes ());
116-
117- assertTrue (maxLikesPerPostType .get (BlogPostType .GUIDE ).isPresent ());
118- assertEquals (20 , maxLikesPerPostType .get (BlogPostType .GUIDE ).get ().getLikes ());
119-
120- assertTrue (maxLikesPerPostType .get (BlogPostType .REVIEW ).isPresent ());
121- assertEquals (15 , maxLikesPerPostType .get (BlogPostType .REVIEW ).get ().getLikes ());
155+ Map <BlogPostType , Optional <BlogPost >> maxLikesPerPostType = posts
156+ .stream ()
157+ .collect (groupingBy (BlogPost ::getType , maxBy (comparingInt (BlogPost ::getLikes ))));
158+
159+ assertTrue (maxLikesPerPostType
160+ .get (BlogPostType .NEWS )
161+ .isPresent ());
162+ assertEquals (35 , maxLikesPerPostType
163+ .get (BlogPostType .NEWS )
164+ .get ()
165+ .getLikes ());
166+
167+ assertTrue (maxLikesPerPostType
168+ .get (BlogPostType .GUIDE )
169+ .isPresent ());
170+ assertEquals (20 , maxLikesPerPostType
171+ .get (BlogPostType .GUIDE )
172+ .get ()
173+ .getLikes ());
174+
175+ assertTrue (maxLikesPerPostType
176+ .get (BlogPostType .REVIEW )
177+ .isPresent ());
178+ assertEquals (15 , maxLikesPerPostType
179+ .get (BlogPostType .REVIEW )
180+ .get ()
181+ .getLikes ());
122182 }
123183
124184 @ Test
125185 public void givenAListOfPosts_whenGroupedByAuthorAndThenByType_thenGetAMapBetweenAuthorAndMapsBetweenTypeAndBlogPosts () {
126- Map <String , Map <BlogPostType , List <BlogPost >>> map = POSTS .stream ().collect (groupingBy (BlogPost ::getAuthor , groupingBy (BlogPost ::getType )));
127-
128- assertEquals (1 , map .get ("Author 1" ).get (BlogPostType .NEWS ).size ());
129- assertEquals (1 , map .get ("Author 1" ).get (BlogPostType .GUIDE ).size ());
130- assertEquals (1 , map .get ("Author 1" ).get (BlogPostType .REVIEW ).size ());
131-
132- assertEquals (1 , map .get ("Author 2" ).get (BlogPostType .NEWS ).size ());
133- assertEquals (1 , map .get ("Author 2" ).get (BlogPostType .REVIEW ).size ());
134- assertNull (map .get ("Author 2" ).get (BlogPostType .GUIDE ));
186+ Map <String , Map <BlogPostType , List <BlogPost >>> map = posts
187+ .stream ()
188+ .collect (groupingBy (BlogPost ::getAuthor , groupingBy (BlogPost ::getType )));
189+
190+ assertEquals (1 , map
191+ .get ("Author 1" )
192+ .get (BlogPostType .NEWS )
193+ .size ());
194+ assertEquals (1 , map
195+ .get ("Author 1" )
196+ .get (BlogPostType .GUIDE )
197+ .size ());
198+ assertEquals (1 , map
199+ .get ("Author 1" )
200+ .get (BlogPostType .REVIEW )
201+ .size ());
202+
203+ assertEquals (1 , map
204+ .get ("Author 2" )
205+ .get (BlogPostType .NEWS )
206+ .size ());
207+ assertEquals (1 , map
208+ .get ("Author 2" )
209+ .get (BlogPostType .REVIEW )
210+ .size ());
211+ assertNull (map
212+ .get ("Author 2" )
213+ .get (BlogPostType .GUIDE ));
135214 }
136215
137216 @ Test
138217 public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary () {
139- Map <BlogPostType , IntSummaryStatistics > likeStatisticsPerType = POSTS .stream ().collect (groupingBy (BlogPost ::getType , summarizingInt (BlogPost ::getLikes )));
218+ Map <BlogPostType , IntSummaryStatistics > likeStatisticsPerType = posts
219+ .stream ()
220+ .collect (groupingBy (BlogPost ::getType , summarizingInt (BlogPost ::getLikes )));
140221
141222 IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType .get (BlogPostType .NEWS );
142223
0 commit comments