Every write copies the whole array, so writes are expensive but iteration needs no lock and never throws ConcurrentModificationException.
var listeners = new CopyOnWriteArrayList<>(List.of("a", "b", "c"));
for (String s : listeners) {
if (s.equals("b")) listeners.add("added-during-iteration");
}
System.out.println("no exception thrown; final list = " + listeners);
System.out.println("size = " + listeners.size());
no exception thrown; final list = [a, b, c, added-during-iteration]
size = 4
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