|
| 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