A fail-fast iterator throws ConcurrentModificationException when it detects that its collection was structurally modified after the iterator was created, other than through the iterator itself. It is a bug detector, not a thread-safety mechanism. The concurrentmodificationexception most often appears when you remove from an ArrayList or HashMap inside a for-each loop.
This guide explains how the check works, shows the typical mistake, and covers the safe alternatives: Iterator.remove, removeIf, copies and concurrent collections.
Collections such as ArrayList, HashMap, HashSet and LinkedList keep an internal counter, usually called modCount. Every structural change (add, remove, clear, anything that changes the size or rehashes) increments it. When you create an iterator, it copies the current modCount as its expected value. On each next() call, the iterator compares the two numbers and throws ConcurrentModificationException if they differ.
Replacing the value of an existing element, such as list.set(i, x) or entry.setValue(v), is not a structural change and does not trigger the exception.
The enhanced for loop uses an iterator behind the scenes. numbers.remove(n) changes the list's modCount, and the iterator notices on the next call to next().
A well-known quirk: if you remove the second-to-last element, the loop ends without an exception, because hasNext() returns false before next() runs the check. Do not rely on this; the behavior is not guaranteed.
Iterator.remove() removes the last element returned by next() and updates the iterator's expected count, so no exception is thrown.
Since Java 8, Collection.removeIf is the shortest and clearest option. A stream filter that builds a new list is also fine when you do not need to change the original.
For maps, use map.entrySet().removeIf(...), map.values().removeIf(...) or map.keySet().removeIf(...). Adding entries while looping over a map is also a structural change, so collect them first and add after the loop, or use computeIfAbsent outside the loop.
A different case is two threads, one iterating while another modifies. Fail-fast detection here is best effort: the modCount is not volatile or locked, so the exception is not guaranteed, and the data can still be corrupted. Do not catch ConcurrentModificationException to "handle" this.
Use a collection designed for it:
| Collection | Iterator behavior |
|---|---|
CopyOnWriteArrayList | Iterates over a snapshot; never throws; best for rare writes |
ConcurrentHashMap | Weakly consistent; never throws; may or may not show concurrent updates |
Collections.synchronizedList | Still fail-fast; you must lock the whole loop yourself |
The name is misleading. It means the collection was modified while being iterated, which can happen in one thread when you call list.remove or list.add inside a for-each loop.
Sometimes, by accident, because hasNext() may return false before the check runs. It is not reliable, so always remove through the iterator or use removeIf.
No. The Javadoc describes the check as best effort, meant for detecting bugs, so never write code that depends on the exception.
Fail-safe or weakly consistent iterators, such as those of CopyOnWriteArrayList and ConcurrentHashMap, which do not throw when the collection changes.
Fail-fast iterators compare a modification counter and throw ConcurrentModificationException when a collection changes structurally during iteration. Remove with Iterator.remove() or removeIf, build a new collection when you can, and use concurrent collections when several threads share data.