immutable sets

This commit is contained in:
mherbaghinyan 2018-12-28 16:26:57 +04:00
parent 68f1cb23dd
commit 8ce227cce7
1 changed files with 44 additions and 0 deletions

View File

@ -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);
}
}