Immutable set creation
Create immutable sets with a single factory call.
Porównanie kodu
✕ Java 8
Set<String> set =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList("a", "b", "c")
)
);
✓ Java 9+
Set<String> set =
Set.of("a", "b", "c");
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
Concise
One line instead of three nested calls.
Detects duplicates
Throws if you accidentally pass duplicate elements.
Immutable
No add/remove possible after creation.
Stare podejście
Verbose Wrapping
Nowoczesne podejście
Set.of()
Od JDK
9
Poziom trudności
Początkujący
Wsparcie JDK
Immutable set creation
Dostępne
Widely available since JDK 9 (Sept 2017)
Jak to działa
Set.of() creates a truly immutable set that rejects nulls and duplicate elements at creation time. No more wrapping mutable sets.
Powiązana dokumentacja