Copying collections immutably
Create an immutable copy of any collection in one call.
Code Comparison
✕ Java 8
List<String> copy =
Collections.unmodifiableList(
new ArrayList<>(original)
);
✓ Java 10+
List<String> copy =
List.copyOf(original);
See a problem with this code? Let us know.
Why the modern way wins
Smart copy
Skips the copy if the source is already immutable.
One call
No manual ArrayList construction + wrapping.
Any Collection
Accepts any Collection as input—no intermediate ArrayList conversion needed.
Old Approach
Manual Copy + Wrap
Modern Approach
List.copyOf()
Since JDK
10
Difficulty
Beginner
JDK Support
Copying collections immutably
Available
Widely available since JDK 10 (March 2018)
How it works
List.copyOf(), Set.copyOf(), and Map.copyOf() create immutable snapshots of existing collections. If the source is already an immutable collection, no copy is made.
Related Documentation
Proof