From c13453d77229fb18475d591ead7fe03e4484f642 Mon Sep 17 00:00:00 2001 From: Tryfon Date: Tue, 7 Feb 2017 12:36:06 +0200 Subject: [PATCH] Java 8 grouping by collector pull request (#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 --- .../java_8_features/groupingby/BlogPost.java | 36 +++++ .../groupingby/BlogPostType.java | 5 + .../Java8GroupingByCollectorUnitTest.java | 132 ++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java create mode 100644 core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java create mode 100644 core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java new file mode 100644 index 0000000000..afc05e356a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java @@ -0,0 +1,36 @@ +package com.baeldung.java_8_features.groupingby; + +public class BlogPost { + private String title; + private String author; + private BlogPostType type; + private int likes; + + public BlogPost(String title, String author, BlogPostType type, int likes) { + this.title = title; + this.author = author; + this.type = type; + this.likes = likes; + } + + public String getTitle() { + return title; + } + + public String getAuthor() { + return author; + } + + public BlogPostType getType() { + return type; + } + + public int getLikes() { + return likes; + } + + @Override + public String toString() { + return "BlogPost{" + "title='" + title + '\'' + ", type=" + type + ", likes=" + likes + '}'; + } +} diff --git a/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java new file mode 100644 index 0000000000..2029784e91 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java @@ -0,0 +1,5 @@ +package com.baeldung.java_8_features.groupingby; + +public enum BlogPostType { + NEWS, REVIEW, GUIDE +} diff --git a/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java b/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java new file mode 100644 index 0000000000..544db90b73 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java @@ -0,0 +1,132 @@ +package com.baeldung.java8; + +import org.junit.Test; + +import com.baeldung.java_8_features.groupingby.BlogPost; +import com.baeldung.java_8_features.groupingby.BlogPostType; + +import java.util.*; +import java.util.concurrent.ConcurrentMap; + +import static java.util.Comparator.comparingInt; +import static java.util.stream.Collectors.*; +import static org.junit.Assert.*; + +public class Java8GroupingByCollectorUnitTest { + + private static final List POSTS = Arrays.asList(new BlogPost("News item 1", "Author 1", BlogPostType.NEWS, 15), new BlogPost("Tech review 1", "Author 2", BlogPostType.REVIEW, 5), + 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)); + + @Test + public void givenAListOfPosts_whenGroupedByType_thenGetAMapBetweenTypeAndPosts() { + Map> postsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType)); + + assertEquals(2, postsPerType.get(BlogPostType.NEWS).size()); + assertEquals(1, postsPerType.get(BlogPostType.GUIDE).size()); + assertEquals(2, postsPerType.get(BlogPostType.REVIEW).size()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndTheirTitlesAreJoinedInAString_thenGetAMapBetweenTypeAndCsvTitles() { + Map postsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, mapping(BlogPost::getTitle, joining(", ", "Post titles: [", "]")))); + + assertEquals("Post titles: [News item 1, News item 2]", postsPerType.get(BlogPostType.NEWS)); + assertEquals("Post titles: [Programming guide]", postsPerType.get(BlogPostType.GUIDE)); + assertEquals("Post titles: [Tech review 1, Tech review 2]", postsPerType.get(BlogPostType.REVIEW)); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndSumTheLikes_thenGetAMapBetweenTypeAndPostLikes() { + Map likesPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes))); + + assertEquals(50, likesPerType.get(BlogPostType.NEWS).intValue()); + assertEquals(20, likesPerType.get(BlogPostType.REVIEW).intValue()); + assertEquals(20, likesPerType.get(BlogPostType.GUIDE).intValue()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeInAnEnumMap_thenGetAnEnumMapBetweenTypeAndPosts() { + EnumMap> postsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, () -> new EnumMap<>(BlogPostType.class), toList())); + + assertEquals(2, postsPerType.get(BlogPostType.NEWS).size()); + assertEquals(1, postsPerType.get(BlogPostType.GUIDE).size()); + assertEquals(2, postsPerType.get(BlogPostType.REVIEW).size()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeInSets_thenGetAMapBetweenTypesAndSetsOfPosts() { + Map> postsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, toSet())); + + assertEquals(2, postsPerType.get(BlogPostType.NEWS).size()); + assertEquals(1, postsPerType.get(BlogPostType.GUIDE).size()); + assertEquals(2, postsPerType.get(BlogPostType.REVIEW).size()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeConcurrently_thenGetAMapBetweenTypeAndPosts() { + ConcurrentMap> postsPerType = POSTS.parallelStream().collect(groupingByConcurrent(BlogPost::getType)); + + assertEquals(2, postsPerType.get(BlogPostType.NEWS).size()); + assertEquals(1, postsPerType.get(BlogPostType.GUIDE).size()); + assertEquals(2, postsPerType.get(BlogPostType.REVIEW).size()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndAveragingLikes_thenGetAMapBetweenTypeAndAverageNumberOfLikes() { + Map averageLikesPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, averagingInt(BlogPost::getLikes))); + + assertEquals(25, averageLikesPerType.get(BlogPostType.NEWS).intValue()); + assertEquals(20, averageLikesPerType.get(BlogPostType.GUIDE).intValue()); + assertEquals(10, averageLikesPerType.get(BlogPostType.REVIEW).intValue()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndCounted_thenGetAMapBetweenTypeAndNumberOfPosts() { + Map numberOfPostsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, counting())); + + assertEquals(2, numberOfPostsPerType.get(BlogPostType.NEWS).intValue()); + assertEquals(1, numberOfPostsPerType.get(BlogPostType.GUIDE).intValue()); + assertEquals(2, numberOfPostsPerType.get(BlogPostType.REVIEW).intValue()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndMaxingLikes_thenGetAMapBetweenTypeAndMaximumNumberOfLikes() { + Map> maxLikesPerPostType = POSTS.stream().collect(groupingBy(BlogPost::getType, maxBy(comparingInt(BlogPost::getLikes)))); + + assertTrue(maxLikesPerPostType.get(BlogPostType.NEWS).isPresent()); + assertEquals(35, maxLikesPerPostType.get(BlogPostType.NEWS).get().getLikes()); + + assertTrue(maxLikesPerPostType.get(BlogPostType.GUIDE).isPresent()); + assertEquals(20, maxLikesPerPostType.get(BlogPostType.GUIDE).get().getLikes()); + + assertTrue(maxLikesPerPostType.get(BlogPostType.REVIEW).isPresent()); + assertEquals(15, maxLikesPerPostType.get(BlogPostType.REVIEW).get().getLikes()); + } + + @Test + public void givenAListOfPosts_whenGroupedByAuthorAndThenByType_thenGetAMapBetweenAuthorAndMapsBetweenTypeAndBlogPosts() { + Map>> map = POSTS.stream().collect(groupingBy(BlogPost::getAuthor, groupingBy(BlogPost::getType))); + + assertEquals(1, map.get("Author 1").get(BlogPostType.NEWS).size()); + assertEquals(1, map.get("Author 1").get(BlogPostType.GUIDE).size()); + assertEquals(1, map.get("Author 1").get(BlogPostType.REVIEW).size()); + + assertEquals(1, map.get("Author 2").get(BlogPostType.NEWS).size()); + assertEquals(1, map.get("Author 2").get(BlogPostType.REVIEW).size()); + assertNull(map.get("Author 2").get(BlogPostType.GUIDE)); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary() { + Map likeStatisticsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, summarizingInt(BlogPost::getLikes))); + + IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType.get(BlogPostType.NEWS); + + assertEquals(2, newsLikeStatistics.getCount()); + assertEquals(50, newsLikeStatistics.getSum()); + assertEquals(25.0, newsLikeStatistics.getAverage(), 0.001); + assertEquals(35, newsLikeStatistics.getMax()); + assertEquals(15, newsLikeStatistics.getMin()); + } + +}