Collections: List.copyOf snapshots, unmodifiableList is a view

This distinction matters for defensive copying: copyOf is safe to hand out, a view still reflects later changes to the source.

Code
var source = new ArrayList<>(List.of("a"));

List<String> snapshot = List.copyOf(source);
List<String> view = Collections.unmodifiableList(source);

source.add("b");
System.out.println("List.copyOf snapshot : " + snapshot);
System.out.println("unmodifiable view    : " + view);
Output
List.copyOf snapshot : [a]
unmodifiable view    : [a, b]
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-30