|
1 | 1 | package com.baeldung.java_8_features.groupingby; |
2 | 2 |
|
3 | | -import com.baeldung.java_8_features.groupingby.BlogPost; |
4 | | -import com.baeldung.java_8_features.groupingby.BlogPostType; |
5 | | -import org.junit.Test; |
6 | | - |
7 | | -import java.util.*; |
| 3 | +import static java.util.Comparator.comparingInt; |
| 4 | +import static java.util.stream.Collectors.averagingInt; |
| 5 | +import static java.util.stream.Collectors.counting; |
| 6 | +import static java.util.stream.Collectors.groupingBy; |
| 7 | +import static java.util.stream.Collectors.groupingByConcurrent; |
| 8 | +import static java.util.stream.Collectors.joining; |
| 9 | +import static java.util.stream.Collectors.mapping; |
| 10 | +import static java.util.stream.Collectors.maxBy; |
| 11 | +import static java.util.stream.Collectors.summarizingInt; |
| 12 | +import static java.util.stream.Collectors.summingInt; |
| 13 | +import static java.util.stream.Collectors.toList; |
| 14 | +import static java.util.stream.Collectors.toSet; |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +import static org.junit.Assert.assertNull; |
| 18 | +import static org.junit.Assert.assertTrue; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.EnumMap; |
| 22 | +import java.util.IntSummaryStatistics; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
8 | 27 | import java.util.concurrent.ConcurrentMap; |
9 | 28 |
|
10 | | -import static java.util.Comparator.comparingInt; |
11 | | -import static java.util.stream.Collectors.*; |
12 | | -import static org.junit.Assert.*; |
| 29 | +import org.junit.Test; |
13 | 30 |
|
14 | 31 | public class Java8GroupingByCollectorUnitTest { |
15 | 32 |
|
@@ -180,4 +197,19 @@ public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBe |
180 | 197 | assertEquals(15, newsLikeStatistics.getMin()); |
181 | 198 | } |
182 | 199 |
|
| 200 | + @Test |
| 201 | + public void givenAListOfPosts_whenGroupedByComplexMapKeyType_thenGetAMapBetweenTupleAndList() { |
| 202 | + Map<Tuple, List<BlogPost>> postsPerTypeAndAuthor = posts.stream() |
| 203 | + .collect(groupingBy(post -> new Tuple(post.getType(), post.getAuthor()))); |
| 204 | + |
| 205 | + List<BlogPost> result = postsPerTypeAndAuthor.get(new Tuple(BlogPostType.GUIDE, "Author 1")); |
| 206 | + |
| 207 | + assertThat(result.size()).isEqualTo(1); |
| 208 | + |
| 209 | + BlogPost blogPost = result.get(0); |
| 210 | + |
| 211 | + assertThat(blogPost.getTitle()).isEqualTo("Programming guide"); |
| 212 | + assertThat(blogPost.getType()).isEqualTo(BlogPostType.GUIDE); |
| 213 | + assertThat(blogPost.getAuthor()).isEqualTo("Author 1"); |
| 214 | + } |
183 | 215 | } |
0 commit comments