Merge pull request #8064 from martinvw/feature/BAEL-3292

[BAEL-3292] Add missing code snippets from the Java 8 groupingBy article
This commit is contained in:
Eric Martin
2019-10-23 19:40:28 -05:00
committed by GitHub
2 changed files with 58 additions and 19 deletions

View File

@@ -1,12 +1,12 @@
package com.baeldung.java_8_features.groupingby; package com.baeldung.java_8_features.groupingby;
public class Tuple { import java.util.Objects;
public class Tuple {
private final BlogPostType type;
private final String author;
private BlogPostType type;
private String author;
public Tuple(BlogPostType type, String author) { public Tuple(BlogPostType type, String author) {
super();
this.type = type; this.type = type;
this.author = author; this.author = author;
} }
@@ -15,20 +15,27 @@ public class Tuple {
return type; return type;
} }
public void setType(BlogPostType type) {
this.type = type;
}
public String getAuthor() { public String getAuthor() {
return author; return author;
} }
public void setAuthor(String author) { @Override
this.author = author; 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 @Override
public String toString() { public String toString() {
return "Tuple [type=" + type + ", author=" + author + ", getType()=" + getType() + ", getAuthor()=" + getAuthor() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; return "Tuple{" + "type=" + type + ", author='" + author + '\'' + '}';
} }
} }

View File

@@ -1,15 +1,32 @@
package com.baeldung.java_8_features.groupingby; package com.baeldung.java_8_features.groupingby;
import com.baeldung.java_8_features.groupingby.BlogPost; import static java.util.Comparator.comparingInt;
import com.baeldung.java_8_features.groupingby.BlogPostType; import static java.util.stream.Collectors.averagingInt;
import org.junit.Test; 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.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.*; 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 java.util.concurrent.ConcurrentMap;
import static java.util.Comparator.comparingInt; import org.junit.Test;
import static java.util.stream.Collectors.*;
import static org.junit.Assert.*;
public class Java8GroupingByCollectorUnitTest { public class Java8GroupingByCollectorUnitTest {
@@ -180,4 +197,19 @@ public class Java8GroupingByCollectorUnitTest {
assertEquals(15, newsLikeStatistics.getMin()); assertEquals(15, newsLikeStatistics.getMin());
} }
@Test
public void givenAListOfPosts_whenGroupedByComplexMapKeyType_thenGetAMapBetweenTupleAndList() {
Map<Tuple, List<BlogPost>> postsPerTypeAndAuthor = posts.stream()
.collect(groupingBy(post -> new Tuple(post.getType(), post.getAuthor())));
List<BlogPost> 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");
}
} }