Immutable list creation
Create immutable lists in one clean expression.
Porównanie kodu
✕ Java 8
List<String> list =
Collections.unmodifiableList(
new ArrayList<>(
Arrays.asList("a", "b", "c")
)
);
✓ Java 9+
List<String> list =
List.of("a", "b", "c");
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
One call
Replace three nested calls with a single factory method.
Truly immutable
Not just a wrapper — the list itself is immutable.
Null-safe
Rejects null elements at creation time, failing fast.
Stare podejście
Verbose Wrapping
Nowoczesne podejście
List.of()
Od JDK
9
Poziom trudności
Początkujący
Wsparcie JDK
Immutable list creation
Dostępne
Widely available since JDK 9 (Sept 2017)
Jak to działa
List.of() creates a truly immutable list — no wrapping, no defensive copy. It's null-hostile (rejects null elements) and structurally immutable. The old way required three nested calls.
Powiązana dokumentacja