Collections beginner

Copying collections immutably

Create an immutable copy of any collection in one call.

✕ Java 8
List<String> copy =
    Collections.unmodifiableList(
        new ArrayList<>(original)
    );
✓ Java 10+
List<String> copy =
    List.copyOf(original);

Smart copy

Skips the copy if the source is already immutable.

📏

One call

No manual ArrayList construction + wrapping.

🛡️

Defensive copy

Changes to the original don't affect the copy.

Old Approach
Manual Copy + Wrap
Modern Approach
List.copyOf()
Since JDK
10
Difficulty
beginner
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.

Share 𝕏 🦋 in