From 640f0d6dde4377ad2c199364665d9c53f022a639 Mon Sep 17 00:00:00 2001
From: mbarriola <85458535+mbarriola@users.noreply.github.com>
Date: Mon, 16 Aug 2021 17:43:53 -0400
Subject: [PATCH] Java 5065 (#11126)
* Commit source code to branch
* BAEL-5065 improvement of groupBy with complex key
---
core-java-modules/core-java-16/pom.xml | 85 +++---
.../java_16_features/groupingby/BlogPost.java | 38 +++
.../groupingby/BlogPostType.java | 5 +
.../java_16_features/groupingby/Tuple.java | 41 +++
.../JavaGroupingByCollectorUnitTest.java | 254 ++++++++++++++++++
5 files changed, 383 insertions(+), 40 deletions(-)
create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/BlogPost.java
create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/BlogPostType.java
create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/Tuple.java
create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/java_16_features/groupingby/JavaGroupingByCollectorUnitTest.java
diff --git a/core-java-modules/core-java-16/pom.xml b/core-java-modules/core-java-16/pom.xml
index 230e342f01..a8a84511db 100644
--- a/core-java-modules/core-java-16/pom.xml
+++ b/core-java-modules/core-java-16/pom.xml
@@ -1,48 +1,53 @@
- 4.0.0
- core-java-16
- 0.1.0-SNAPSHOT
- core-java-16
- jar
- http://maven.apache.org
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
+ core-java-16
+ 0.1.0-SNAPSHOT
+ core-java-16
+ jar
+ http://maven.apache.org
-
- com.baeldung
- parent-modules
- 1.0.0-SNAPSHOT
- ../../
-
+
+ com.baeldung
+ parent-modules
+ 1.0.0-SNAPSHOT
+ ../../
+
-
-
- org.assertj
- assertj-core
- ${assertj.version}
- test
-
-
+
+
+ org.assertj
+ assertj-core
+ ${assertj.version}
+ test
+
+
+ org.apache.commons
+ commons-lang3
+ 3.12.0
+
+
-
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
- ${maven-compiler-plugin.version}
-
-
- ${maven.compiler.target.version}
-
-
-
-
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven-compiler-plugin.version}
+
+
+ ${maven.compiler.target.version}
+
+
+
+
-
- 16
- 16
- 3.6.1
-
+
+ 16
+ 16
+ 3.6.1
+
\ No newline at end of file
diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/BlogPost.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/BlogPost.java
new file mode 100644
index 0000000000..adaa1044ff
--- /dev/null
+++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/BlogPost.java
@@ -0,0 +1,38 @@
+package com.baeldung.java_16_features.groupingby;
+
+public class BlogPost {
+
+ private String title;
+ private String author;
+ private BlogPostType type;
+ private int likes;
+ record AuthPostTypesLikes(String author, BlogPostType type, 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-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/BlogPostType.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/BlogPostType.java
new file mode 100644
index 0000000000..df38b7e1c4
--- /dev/null
+++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/BlogPostType.java
@@ -0,0 +1,5 @@
+package com.baeldung.java_16_features.groupingby;
+
+public enum BlogPostType {
+ NEWS, REVIEW, GUIDE
+}
diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/Tuple.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/Tuple.java
new file mode 100644
index 0000000000..ad41207aa4
--- /dev/null
+++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/groupingby/Tuple.java
@@ -0,0 +1,41 @@
+package com.baeldung.java_16_features.groupingby;
+
+import java.util.Objects;
+
+public class Tuple {
+ private final BlogPostType type;
+ private final String author;
+
+ public Tuple(BlogPostType type, String author) {
+ this.type = type;
+ this.author = author;
+ }
+
+ public BlogPostType getType() {
+ return type;
+ }
+
+ public String getAuthor() {
+ return author;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o)
+ return true;
+ if (o == null || getClass() != o.getClass())
+ return false;
+ Tuple tuple = (Tuple) o;
+ return type == tuple.type && author.equals(tuple.author);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(type, author);
+ }
+
+ @Override
+ public String toString() {
+ return "Tuple{" + "type=" + type + ", author='" + author + '\'' + '}';
+ }
+}
diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/java_16_features/groupingby/JavaGroupingByCollectorUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/java_16_features/groupingby/JavaGroupingByCollectorUnitTest.java
new file mode 100644
index 0000000000..0e926246ff
--- /dev/null
+++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/java_16_features/groupingby/JavaGroupingByCollectorUnitTest.java
@@ -0,0 +1,254 @@
+package com.baeldung.java_16_features.groupingby;
+
+import static java.util.Comparator.comparingInt;
+import static java.util.stream.Collectors.averagingInt;
+import static java.util.stream.Collectors.counting;
+import static java.util.stream.Collectors.groupingBy;
+import static java.util.stream.Collectors.groupingByConcurrent;
+import static java.util.stream.Collectors.joining;
+import static java.util.stream.Collectors.mapping;
+import static java.util.stream.Collectors.maxBy;
+import static java.util.stream.Collectors.summarizingInt;
+import static java.util.stream.Collectors.summingInt;
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.Collectors.toSet;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.IntSummaryStatistics;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.junit.jupiter.api.Test;
+
+public class JavaGroupingByCollectorUnitTest {
+
+ 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());
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByComplexMapPairKeyType_thenGetAMapBetweenPairAndList() {
+
+ Map, List> postsPerTypeAndAuthor = posts.stream()
+ .collect(groupingBy(post -> new ImmutablePair<>(post.getType(), post.getAuthor())));
+
+ List result = postsPerTypeAndAuthor.get(new ImmutablePair<>(BlogPostType.GUIDE, "Author 1"));
+
+ assertThat(result.size()).isEqualTo(1);
+
+ BlogPost blogPost = result.get(0);
+
+ assertThat(blogPost.getTitle()).isEqualTo("Programming guide");
+ assertThat(blogPost.getType()).isEqualTo(BlogPostType.GUIDE);
+ assertThat(blogPost.getAuthor()).isEqualTo("Author 1");
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByComplexMapKeyType_thenGetAMapBetweenTupleAndList() {
+
+ Map> postsPerTypeAndAuthor = posts.stream()
+ .collect(groupingBy(post -> new Tuple(post.getType(), post.getAuthor())));
+
+ List result = postsPerTypeAndAuthor.get(new Tuple(BlogPostType.GUIDE, "Author 1"));
+
+ assertThat(result.size()).isEqualTo(1);
+
+ BlogPost blogPost = result.get(0);
+
+ assertThat(blogPost.getTitle()).isEqualTo("Programming guide");
+ assertThat(blogPost.getType()).isEqualTo(BlogPostType.GUIDE);
+ assertThat(blogPost.getAuthor()).isEqualTo("Author 1");
+ }
+
+ @Test
+ public void givenAListOfPosts_whenGroupedByRecord_thenGetAMapBetweenRecordAndList() {
+
+ Map> postsPerTypeAndAuthor = posts.stream()
+ .collect(groupingBy(post -> new BlogPost.AuthPostTypesLikes(post.getAuthor(), post.getType(), post.getLikes())));
+
+ List result = postsPerTypeAndAuthor.get(new BlogPost.AuthPostTypesLikes("Author 1", BlogPostType.GUIDE, 20));
+
+ assertThat(result.size()).isEqualTo(1);
+
+ BlogPost blogPost = result.get(0);
+
+ assertThat(blogPost.getTitle()).isEqualTo("Programming guide");
+ assertThat(blogPost.getType()).isEqualTo(BlogPostType.GUIDE);
+ assertThat(blogPost.getAuthor()).isEqualTo("Author 1");
+ assertThat(blogPost.getLikes()).isEqualTo(20);
+ }
+
+}