Maps: Map.copyOf snapshots, unmodifiableMap wraps

Map.copyOf takes an independent copy, while Collections.unmodifiableMap is a live view - later changes to the backing map show through the view.

Code
Map<String, Integer> source = new TreeMap<>();
source.put("a", 1);

Map<String, Integer> snapshot = Map.copyOf(source);
Map<String, Integer> view = Collections.unmodifiableMap(source);

source.put("b", 2);
System.out.println("copyOf snapshot   : " + new TreeMap<>(snapshot));
System.out.println("unmodifiable view : " + view);
Output
copyOf snapshot   : {a=1}
unmodifiable view : {a=1, b=2}
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