1- package com .baeldung .streams . collectors ;
1+ package com .baeldung .collectors ;
22
33import com .google .common .collect .ImmutableList ;
44import com .google .common .collect .ImmutableSet ;
55import com .google .common .collect .Sets ;
66import 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 .*;
169import java .util .function .BiConsumer ;
1710import java .util .function .BinaryOperator ;
1811import java .util .function .Function ;
1912import java .util .function .Supplier ;
2013import java .util .stream .Collector ;
2114
2215import 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 .*;
3617import static org .assertj .core .api .Assertions .assertThat ;
3718import 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 (
0 commit comments