Skip to content

Commit 78c9be2

Browse files
authored
BAEL-4589: Update Guide to Java 8's Collectors (eugenp#10009)
* BAEL-4589: Move Guide to Java 8’s Collectors to core-java-11-2 * BAEL-4589: Add Java 10 unmodifiable* collectors examples
1 parent 11a7730 commit 78c9be2

4 files changed

Lines changed: 35 additions & 23 deletions

File tree

core-java-modules/core-java-11-2/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ This module contains articles about Java 11 core features
44

55
### Relevant articles
66
- [Guide to Java 8 Optional](https://www.baeldung.com/java-optional)
7+
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
78

core-java-modules/core-java-11-2/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
</parent>
1818

1919
<dependencies>
20+
<dependency>
21+
<groupId>com.google.guava</groupId>
22+
<artifactId>guava</artifactId>
23+
<version>${guava.version}</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.assertj</groupId>
2227
<artifactId>assertj-core</artifactId>
@@ -42,6 +47,7 @@
4247
<properties>
4348
<maven.compiler.source.version>11</maven.compiler.source.version>
4449
<maven.compiler.target.version>11</maven.compiler.target.version>
50+
<guava.version>29.0-jre</guava.version>
4551
<assertj.version>3.17.2</assertj.version>
4652
</properties>
4753

core-java-modules/core-java-streams-3/src/test/java/com/baeldung/streams/collectors/Java8CollectorsUnitTest.java renamed to core-java-modules/core-java-11-2/src/test/java/com/baeldung/collectors/Java8CollectorsUnitTest.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,19 @@
1-
package com.baeldung.streams.collectors;
1+
package com.baeldung.collectors;
22

33
import com.google.common.collect.ImmutableList;
44
import com.google.common.collect.ImmutableSet;
55
import com.google.common.collect.Sets;
66
import org.junit.Test;
77

8-
import java.util.Arrays;
9-
import java.util.Comparator;
10-
import java.util.DoubleSummaryStatistics;
11-
import java.util.LinkedList;
12-
import java.util.List;
13-
import java.util.Map;
14-
import java.util.Optional;
15-
import java.util.Set;
8+
import java.util.*;
169
import java.util.function.BiConsumer;
1710
import java.util.function.BinaryOperator;
1811
import java.util.function.Function;
1912
import java.util.function.Supplier;
2013
import java.util.stream.Collector;
2114

2215
import static com.google.common.collect.Sets.newHashSet;
23-
import static java.util.stream.Collectors.averagingDouble;
24-
import static java.util.stream.Collectors.collectingAndThen;
25-
import static java.util.stream.Collectors.counting;
26-
import static java.util.stream.Collectors.groupingBy;
27-
import static java.util.stream.Collectors.joining;
28-
import static java.util.stream.Collectors.maxBy;
29-
import static java.util.stream.Collectors.partitioningBy;
30-
import static java.util.stream.Collectors.summarizingDouble;
31-
import static java.util.stream.Collectors.summingDouble;
32-
import static java.util.stream.Collectors.toCollection;
33-
import static java.util.stream.Collectors.toList;
34-
import static java.util.stream.Collectors.toMap;
35-
import static java.util.stream.Collectors.toSet;
16+
import static java.util.stream.Collectors.*;
3617
import static org.assertj.core.api.Assertions.assertThat;
3718
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3819

@@ -48,13 +29,29 @@ public void whenCollectingToList_shouldCollectToList() throws Exception {
4829
assertThat(result).containsAll(givenList);
4930
}
5031

32+
@Test
33+
public void whenCollectingToUnmodifiableList_shouldCollectToUnmodifiableList() {
34+
final List<String> result = givenList.stream().collect(toUnmodifiableList());
35+
36+
assertThatThrownBy(() -> result.add("foo"))
37+
.isInstanceOf(UnsupportedOperationException.class);
38+
}
39+
5140
@Test
5241
public void whenCollectingToSet_shouldCollectToSet() throws Exception {
5342
final Set<String> result = givenList.stream().collect(toSet());
5443

5544
assertThat(result).containsAll(givenList);
5645
}
5746

47+
@Test
48+
public void whenCollectingToUnmodifiableSet_shouldCollectToUnmodifiableSet() {
49+
final Set<String> result = givenList.stream().collect(toUnmodifiableSet());
50+
51+
assertThatThrownBy(() -> result.add("foo"))
52+
.isInstanceOf(UnsupportedOperationException.class);
53+
}
54+
5855
@Test
5956
public void givenContainsDuplicateElements_whenCollectingToSet_shouldAddDuplicateElementsOnlyOnce() throws Exception {
6057
final Set<String> result = listWithDuplicates.stream().collect(toSet());
@@ -84,6 +81,15 @@ public void whenCollectingToMap_shouldCollectToMap() throws Exception {
8481
assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("ccc", 3).containsEntry("dd", 2);
8582
}
8683

84+
@Test
85+
public void whenCollectingToUnmodifiableMap_shouldCollectToUnmodifiableMap() {
86+
final Map<String, Integer> result = givenList.stream()
87+
.collect(toUnmodifiableMap(Function.identity(), String::length));
88+
89+
assertThatThrownBy(() -> result.put("foo", 3))
90+
.isInstanceOf(UnsupportedOperationException.class);
91+
}
92+
8793
@Test
8894
public void whenCollectingToMapwWithDuplicates_shouldCollectToMapMergingTheIdenticalItems() throws Exception {
8995
final Map<String, Integer> result = listWithDuplicates.stream().collect(

core-java-modules/core-java-streams-3/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ This module contains articles about the Stream API in Java.
66
- [The Difference Between map() and flatMap()](https://www.baeldung.com/java-difference-map-and-flatmap)
77
- [How to Use if/else Logic in Java 8 Streams](https://www.baeldung.com/java-8-streams-if-else-logic)
88
- [The Difference Between Collection.stream().forEach() and Collection.forEach()](https://www.baeldung.com/java-collection-stream-foreach)
9-
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
109
- [Primitive Type Streams in Java 8](https://www.baeldung.com/java-8-primitive-streams)
1110
- [Debugging Java 8 Streams with IntelliJ](https://www.baeldung.com/intellij-debugging-java-streams)
1211
- [Add BigDecimals using the Stream API](https://www.baeldung.com/java-stream-add-bigdecimals)

0 commit comments

Comments
 (0)