immutable sets
This commit is contained in:
parent
68f1cb23dd
commit
8ce227cce7
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.set;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class UnmodifiableSet {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Set<String> set = new HashSet<>();
|
||||||
|
set.add("Canada");
|
||||||
|
set.add("USA");
|
||||||
|
|
||||||
|
coreJDK(set);
|
||||||
|
guavaOf();
|
||||||
|
copyOf(set);
|
||||||
|
setOf();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setOf() {
|
||||||
|
Set<String> immutable = Set.of("Canada", "USA");
|
||||||
|
System.out.println(immutable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void guavaOf() {
|
||||||
|
Set<String> immutable = ImmutableSet.of("Canada", "USA");
|
||||||
|
System.out.println(immutable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void copyOf(Set<String> set) {
|
||||||
|
Set<String> immutable = ImmutableSet.copyOf(set);
|
||||||
|
set.add("Costa Rica");
|
||||||
|
System.out.println(immutable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void coreJDK(Set<String> set) {
|
||||||
|
Set<String> unmodifiableSet = Collections.unmodifiableSet(set);
|
||||||
|
set.add("Costa Rica");
|
||||||
|
System.out.println(unmodifiableSet);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user