Immutable list creation
Code Comparison
List<String> list =
Collections.unmodifiableList(
new ArrayList<>(
Arrays.asList("a", "b", "c")
)
);
List<String> list =
List.of("a", "b", "c");
Why the modern way wins
๐
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.
Old Approach
Verbose Wrapping
Modern Approach
List.of()
JDK Support
Immutable list creation
Available
Widely available since JDK 9 (Sept 2017)
How it works
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.
Related Documentation
Proof