|
1 | 1 | package com.baeldung.java8; |
2 | 2 |
|
| 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.junit.Assert.assertEquals; |
| 16 | +import static org.junit.Assert.assertNull; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.EnumMap; |
| 21 | +import java.util.IntSummaryStatistics; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.concurrent.ConcurrentMap; |
| 27 | + |
3 | 28 | import org.junit.Test; |
4 | 29 |
|
5 | 30 | import com.baeldung.java_8_features.groupingby.BlogPost; |
6 | 31 | import com.baeldung.java_8_features.groupingby.BlogPostType; |
7 | 32 |
|
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 | 33 | public class Java8GroupingByCollectorUnitTest { |
16 | 34 |
|
17 | 35 | 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), |
@@ -119,9 +137,9 @@ public void givenAListOfPosts_whenGroupedByAuthorAndThenByType_thenGetAMapBetwee |
119 | 137 | @Test |
120 | 138 | public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary() { |
121 | 139 | Map<BlogPostType, IntSummaryStatistics> likeStatisticsPerType = POSTS.stream().collect(groupingBy(BlogPost::getType, summarizingInt(BlogPost::getLikes))); |
122 | | - |
| 140 | + |
123 | 141 | IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType.get(BlogPostType.NEWS); |
124 | | - |
| 142 | + |
125 | 143 | assertEquals(2, newsLikeStatistics.getCount()); |
126 | 144 | assertEquals(50, newsLikeStatistics.getSum()); |
127 | 145 | assertEquals(25.0, newsLikeStatistics.getAverage(), 0.001); |
|
0 commit comments