Skip to content

Commit a42fc70

Browse files
committed
[BAEL-3292] Add missing code snippets from the Java 8 groupingBy article
The Tuple had to be changed because it required an equals/hashcode Also: formatted with eclipse profile
1 parent bcf119b commit a42fc70

2 files changed

Lines changed: 58 additions & 19 deletions

File tree

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.baeldung.java_8_features.groupingby;
22

3+
import java.util.Objects;
4+
35
public class Tuple {
6+
private final BlogPostType type;
7+
private final String author;
48

5-
private BlogPostType type;
6-
private String author;
7-
89
public Tuple(BlogPostType type, String author) {
9-
super();
1010
this.type = type;
1111
this.author = author;
1212
}
@@ -15,20 +15,27 @@ public BlogPostType getType() {
1515
return type;
1616
}
1717

18-
public void setType(BlogPostType type) {
19-
this.type = type;
20-
}
21-
2218
public String getAuthor() {
2319
return author;
2420
}
2521

26-
public void setAuthor(String author) {
27-
this.author = author;
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o)
25+
return true;
26+
if (o == null || getClass() != o.getClass())
27+
return false;
28+
Tuple tuple = (Tuple) o;
29+
return type == tuple.type && author.equals(tuple.author);
30+
}
31+
32+
@Override
33+
public int hashCode() {
34+
return Objects.hash(type, author);
2835
}
2936

3037
@Override
3138
public String toString() {
32-
return "Tuple [type=" + type + ", author=" + author + ", getType()=" + getType() + ", getAuthor()=" + getAuthor() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
39+
return "Tuple{" + "type=" + type + ", author='" + author + '\'' + '}';
3340
}
3441
}

core-java-modules/core-java-8/src/test/java/com/baeldung/java_8_features/groupingby/Java8GroupingByCollectorUnitTest.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
package com.baeldung.java_8_features.groupingby;
22

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;
827
import java.util.concurrent.ConcurrentMap;
928

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;
1330

1431
public class Java8GroupingByCollectorUnitTest {
1532

@@ -180,4 +197,19 @@ public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBe
180197
assertEquals(15, newsLikeStatistics.getMin());
181198
}
182199

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+
}
183215
}

0 commit comments

Comments
 (0)