Skip to content

Commit c13453d

Browse files
Tryfonsunilmogadati
authored andcommitted
Java 8 grouping by collector pull request (eugenp#1102)
* Char array to string and string to char array test cases added * Minor code renames * Added groupingBy collector unit tests * Added test case for int summary calculation on grouped results * Added the grouping by classes to the main source path * Reverting char array to string test class * Reverting char array to string test class * Reverting char array to string test class * Reverting char array to string test class
1 parent 178f49e commit c13453d

3 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.java_8_features.groupingby;
2+
3+
public class BlogPost {
4+
private String title;
5+
private String author;
6+
private BlogPostType type;
7+
private int likes;
8+
9+
public BlogPost(String title, String author, BlogPostType type, int likes) {
10+
this.title = title;
11+
this.author = author;
12+
this.type = type;
13+
this.likes = likes;
14+
}
15+
16+
public String getTitle() {
17+
return title;
18+
}
19+
20+
public String getAuthor() {
21+
return author;
22+
}
23+
24+
public BlogPostType getType() {
25+
return type;
26+
}
27+
28+
public int getLikes() {
29+
return likes;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "BlogPost{" + "title='" + title + '\'' + ", type=" + type + ", likes=" + likes + '}';
35+
}
36+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.java_8_features.groupingby;
2+
3+
public enum BlogPostType {
4+
NEWS, REVIEW, GUIDE
5+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.baeldung.java8;
2+
3+
import org.junit.Test;
4+
5+
import com.baeldung.java_8_features.groupingby.BlogPost;
6+
import com.baeldung.java_8_features.groupingby.BlogPostType;
7+
8+
import java.util.*;
9+
import java.util.concurrent.ConcurrentMap;
10+
11+
import static java.util.Comparator.comparingInt;
12+
import static java.util.stream.Collectors.*;
13+
import static org.junit.Assert.*;
14+
15+
public class Java8GroupingByCollectorUnitTest {
16+
17+
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),
18+
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));
19+
20+
@Test
21+
public void givenAListOfPosts_whenGroupedByType_thenGetAMapBetweenTypeAndPosts() {
22+
Map<BlogPostType, List<BlogPost>> postsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType));
23+
24+
assertEquals(2, postsPerType.get(BlogPostType.NEWS).size());
25+
assertEquals(1, postsPerType.get(BlogPostType.GUIDE).size());
26+
assertEquals(2, postsPerType.get(BlogPostType.REVIEW).size());
27+
}
28+
29+
@Test
30+
public void givenAListOfPosts_whenGroupedByTypeAndTheirTitlesAreJoinedInAString_thenGetAMapBetweenTypeAndCsvTitles() {
31+
Map<BlogPostType, String> postsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, mapping(BlogPost::getTitle, joining(", ", "Post titles: [", "]"))));
32+
33+
assertEquals("Post titles: [News item 1, News item 2]", postsPerType.get(BlogPostType.NEWS));
34+
assertEquals("Post titles: [Programming guide]", postsPerType.get(BlogPostType.GUIDE));
35+
assertEquals("Post titles: [Tech review 1, Tech review 2]", postsPerType.get(BlogPostType.REVIEW));
36+
}
37+
38+
@Test
39+
public void givenAListOfPosts_whenGroupedByTypeAndSumTheLikes_thenGetAMapBetweenTypeAndPostLikes() {
40+
Map<BlogPostType, Integer> likesPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes)));
41+
42+
assertEquals(50, likesPerType.get(BlogPostType.NEWS).intValue());
43+
assertEquals(20, likesPerType.get(BlogPostType.REVIEW).intValue());
44+
assertEquals(20, likesPerType.get(BlogPostType.GUIDE).intValue());
45+
}
46+
47+
@Test
48+
public void givenAListOfPosts_whenGroupedByTypeInAnEnumMap_thenGetAnEnumMapBetweenTypeAndPosts() {
49+
EnumMap<BlogPostType, List<BlogPost>> postsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, () -> new EnumMap<>(BlogPostType.class), toList()));
50+
51+
assertEquals(2, postsPerType.get(BlogPostType.NEWS).size());
52+
assertEquals(1, postsPerType.get(BlogPostType.GUIDE).size());
53+
assertEquals(2, postsPerType.get(BlogPostType.REVIEW).size());
54+
}
55+
56+
@Test
57+
public void givenAListOfPosts_whenGroupedByTypeInSets_thenGetAMapBetweenTypesAndSetsOfPosts() {
58+
Map<BlogPostType, Set<BlogPost>> postsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, toSet()));
59+
60+
assertEquals(2, postsPerType.get(BlogPostType.NEWS).size());
61+
assertEquals(1, postsPerType.get(BlogPostType.GUIDE).size());
62+
assertEquals(2, postsPerType.get(BlogPostType.REVIEW).size());
63+
}
64+
65+
@Test
66+
public void givenAListOfPosts_whenGroupedByTypeConcurrently_thenGetAMapBetweenTypeAndPosts() {
67+
ConcurrentMap<BlogPostType, List<BlogPost>> postsPerType = POSTS.parallelStream().collect(groupingByConcurrent(BlogPost::getType));
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());
72+
}
73+
74+
@Test
75+
public void givenAListOfPosts_whenGroupedByTypeAndAveragingLikes_thenGetAMapBetweenTypeAndAverageNumberOfLikes() {
76+
Map<BlogPostType, Double> averageLikesPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, averagingInt(BlogPost::getLikes)));
77+
78+
assertEquals(25, averageLikesPerType.get(BlogPostType.NEWS).intValue());
79+
assertEquals(20, averageLikesPerType.get(BlogPostType.GUIDE).intValue());
80+
assertEquals(10, averageLikesPerType.get(BlogPostType.REVIEW).intValue());
81+
}
82+
83+
@Test
84+
public void givenAListOfPosts_whenGroupedByTypeAndCounted_thenGetAMapBetweenTypeAndNumberOfPosts() {
85+
Map<BlogPostType, Long> numberOfPostsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, counting()));
86+
87+
assertEquals(2, numberOfPostsPerType.get(BlogPostType.NEWS).intValue());
88+
assertEquals(1, numberOfPostsPerType.get(BlogPostType.GUIDE).intValue());
89+
assertEquals(2, numberOfPostsPerType.get(BlogPostType.REVIEW).intValue());
90+
}
91+
92+
@Test
93+
public void givenAListOfPosts_whenGroupedByTypeAndMaxingLikes_thenGetAMapBetweenTypeAndMaximumNumberOfLikes() {
94+
Map<BlogPostType, Optional<BlogPost>> maxLikesPerPostType = POSTS.stream().collect(groupingBy(BlogPost::getType, maxBy(comparingInt(BlogPost::getLikes))));
95+
96+
assertTrue(maxLikesPerPostType.get(BlogPostType.NEWS).isPresent());
97+
assertEquals(35, maxLikesPerPostType.get(BlogPostType.NEWS).get().getLikes());
98+
99+
assertTrue(maxLikesPerPostType.get(BlogPostType.GUIDE).isPresent());
100+
assertEquals(20, maxLikesPerPostType.get(BlogPostType.GUIDE).get().getLikes());
101+
102+
assertTrue(maxLikesPerPostType.get(BlogPostType.REVIEW).isPresent());
103+
assertEquals(15, maxLikesPerPostType.get(BlogPostType.REVIEW).get().getLikes());
104+
}
105+
106+
@Test
107+
public void givenAListOfPosts_whenGroupedByAuthorAndThenByType_thenGetAMapBetweenAuthorAndMapsBetweenTypeAndBlogPosts() {
108+
Map<String, Map<BlogPostType, List<BlogPost>>> map = POSTS.stream().collect(groupingBy(BlogPost::getAuthor, groupingBy(BlogPost::getType)));
109+
110+
assertEquals(1, map.get("Author 1").get(BlogPostType.NEWS).size());
111+
assertEquals(1, map.get("Author 1").get(BlogPostType.GUIDE).size());
112+
assertEquals(1, map.get("Author 1").get(BlogPostType.REVIEW).size());
113+
114+
assertEquals(1, map.get("Author 2").get(BlogPostType.NEWS).size());
115+
assertEquals(1, map.get("Author 2").get(BlogPostType.REVIEW).size());
116+
assertNull(map.get("Author 2").get(BlogPostType.GUIDE));
117+
}
118+
119+
@Test
120+
public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary() {
121+
Map<BlogPostType, IntSummaryStatistics> likeStatisticsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, summarizingInt(BlogPost::getLikes)));
122+
123+
IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType.get(BlogPostType.NEWS);
124+
125+
assertEquals(2, newsLikeStatistics.getCount());
126+
assertEquals(50, newsLikeStatistics.getSum());
127+
assertEquals(25.0, newsLikeStatistics.getAverage(), 0.001);
128+
assertEquals(35, newsLikeStatistics.getMax());
129+
assertEquals(15, newsLikeStatistics.getMin());
130+
}
131+
132+
}

0 commit comments

Comments
 (0)