Collections Początkujący

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);
Widzisz problem z tym kodem? Daj nam znać.

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.

Stare podejście
Manual Copy + Wrap
Nowoczesne podejście
List.copyOf()
Od JDK
10
Poziom trudności
Początkujący
Copying collections immutably
Dostępne

Widely available since JDK 10 (March 2018)

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.

Udostępnij 𝕏 🦋 in