import java.util.*;
class Demo {
static final List<String> ROLES = List.of("admin", "editor", "viewer");
static final Set<String> PERMS = Set.of("read", "write", "delete");
static final Map<String, Integer> LIMITS = Map.of(
"free", 10,
"hobby", 100,
"pro", 10000
);
void example() {
// All throw on mutation attempts
// ROLES.add("guest"); // UnsupportedOperationException
// For maps with more than 10 entries, use Map.ofEntries:
var huge = Map.ofEntries(
Map.entry("a", 1),
Map.entry("b", 2),
// ... up to as many as you like
Map.entry("z", 26)
);
// Copy any existing collection to immutable form
List<Integer> mutable = new ArrayList<>(List.of(1, 2, 3));
List<Integer> frozen = List.copyOf(mutable); // snapshot, immutable
System.out.println(frozen);
}
}
Create a free account and build your private vault. Share publicly whenever you want.