Java: Immutable copy with List.copyOf

List.copyOf snapshots a collection into an unmodifiable list.

Code
List<Integer> src = new ArrayList<>(List.of(1, 2, 3));
List<Integer> copy = List.copyOf(src);
src.add(4);
System.out.println("src=" + src + " copy=" + copy);
Output
src=[1, 2, 3, 4] copy=[1, 2, 3]
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-26