What is the difference between fail-fast and fail-safe iterators?
The terms fail-fast and fail-safe describe how iterators behave when the underlying collection is modified during iteration.
**Fail-fast iterators** throw `ConcurrentModificationException` if the collection is structurally modified (elements added or removed) after the iterator is created, by any means other than the iterator's own `remove()` method. They detect modification by checking a `modCount` field on the collection against a count captured when the iterator was created. If they differ, the exception is thrown on the next `next()` or `hasNext()` call.
Fail-fast iterators are used by most standard `java.util` collections: `ArrayList`, `HashMap`, `HashSet`, `LinkedList`. They are not a thread-safety guarantee — the exception is a best-effort notification. The Java documentation explicitly states that fail-fast behavior should not be relied upon for program correctness; it is designed to detect programming bugs, not as a concurrency mechanism.
**Fail-safe iterators** work on a snapshot or copy of the collection as it existed when iteration began. Modifications to the original collection during iteration are not visible to the iterator and do not cause exceptions. The tradeoff is that the iterator may not reflect the current state of the collection.
Fail-safe iterators are used by `java.util.concurrent` classes: `ConcurrentHashMap` (iterator reflects current state via a weakly consistent traversal), `CopyOnWriteArrayList` (iterates over a snapshot of the array taken at iterator creation time — true copy semantics).
`CopyOnWriteArrayList` is appropriate for read-heavy workloads with infrequent writes — each write copies the entire array, which is expensive but produces a pristine read path with no locking. `ConcurrentHashMap`'s iterators are weakly consistent, meaning they may or may not reflect updates that occur after iteration begins.
For interviews: `ArrayList` → fail-fast; `CopyOnWriteArrayList` → fail-safe (copy). Be precise about weakly consistent iterators in `ConcurrentHashMap`.
| Aspect | Fail-Fast | Fail-Safe |
|---|---|---|
| On modification | Throws ConcurrentModificationException | No exception — iterates over copy/snapshot |
| Detection mechanism | modCount comparison | Operates on copy or weakly consistent view |
| Reflects live data? | Yes, until modification | No (copy) or partially (weakly consistent) |
| Memory overhead | Low | Higher (copy of collection) |
| Examples | ArrayList, HashMap, HashSet | CopyOnWriteArrayList, ConcurrentHashMap |
| Thread-safe? | No — exception is best-effort | Yes — designed for concurrent use |
Correctly defines both types, explains ConcurrentModificationException, and names at least one concrete example of each.
Also explains the modCount mechanism, the snapshot/copy strategy, the memory cost of CopyOnWriteArrayList, and the weakly consistent distinction for ConcurrentHashMap.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment