File tree 2 files changed +15
-3
lines changed
main/java/com/baeldung/java9/set
test/java/com/baeldung/java9
2 files changed +15
-3
lines changed Original file line number Diff line number Diff line change 1
- package com .baeldung .set ;
1
+ package com .baeldung .java9 . set ;
2
2
3
3
import com .google .common .collect .ImmutableSet ;
4
4
@@ -21,8 +21,8 @@ public static void main(String[] args) {
21
21
}
22
22
23
23
private static void java9Of () {
24
- // Set<String> immutable = Set.of("Canada", "USA");
25
- // System.out.println(immutable);
24
+ Set <String > immutable = Set .of ("Canada" , "USA" );
25
+ System .out .println (immutable );
26
26
}
27
27
28
28
private static void guavaOf () {
Original file line number Diff line number Diff line change 1
1
package com .baeldung .java9 ;
2
2
3
+ import java .util .Collections ;
4
+ import java .util .HashSet ;
3
5
import java .util .Set ;
4
6
import org .junit .Test ;
5
7
@@ -23,4 +25,14 @@ public void testArrayToSet() {
23
25
Set <Integer > intSet = Set .of (intArray );
24
26
assertEquals (intSet .size (), intArray .length );
25
27
}
28
+
29
+ @ Test (expected = UnsupportedOperationException .class )
30
+ public void testUnmodifiableSet () {
31
+ Set <String > set = new HashSet <>();
32
+ set .add ("Canada" );
33
+ set .add ("USA" );
34
+
35
+ Set <String > unmodifiableSet = Collections .unmodifiableSet (set );
36
+ unmodifiableSet .add ("Costa Rica" );
37
+ }
26
38
}
You can’t perform that action at this time.
0 commit comments