Quick revision
The short answer
Fail-fast iterators, such as those of ArrayList and HashMap, compare the collection's modCount with the value they started with and throw ConcurrentModificationException when the collection was structurally changed other than through the iterator; this check is best-effort and only a bug detector. CopyOnWriteArrayList iterators instead walk a snapshot array, never throw and never see later changes, and ConcurrentHashMap iterators are weakly consistent: they never throw and may or may not reflect concurrent updates. Fail-safe is only an informal name for these last two.
What to remember
In this answer, we saw that fail-fast iterators in ArrayList and HashMap detect structural changes made outside the iterator through modCount and throw ConcurrentModificationException, but only on a best-effort basis, so the exception is a bug detector rather than a guarantee. CopyOnWriteArrayList iterators walk a snapshot array, never throw and never see later changes, at the cost of copying on every write. ConcurrentHashMap iterators are weakly consistent, never throw and may or may not show concurrent updates. Fail-safe is an informal name for these last two, and the Javadoc's precise terms are snapshot and weakly consistent.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Fail-fast iterators, such as those of ArrayList and HashMap, compare the collection's modCount with the value they started with and throw ConcurrentModificationException when the collection was structurally changed other than through the iterator; this check is best-effort and only a bug detector. CopyOnWriteArrayList iterators instead walk a snapshot array, never throw and never see later changes, and ConcurrentHashMap iterators are weakly consistent: they never throw and may or may not reflect concurrent updates. Fail-safe is only an informal name for these last two.
Why this question is asked
Every Java developer has seen ConcurrentModificationException, and many have fixed it by swapping in a concurrent collection without knowing what they gave up. Interviewers ask this question to see whether we understand that the collections make three different promises during iteration, and whether we know which promise we are relying on. In this answer, we will see how fail-fast detection works and why it is only best-effort, then look at the snapshot iterators of CopyOnWriteArrayList and the weakly consistent iterators of ConcurrentHashMap, and finish with a comparison and a note on the term fail-safe.
1How Fail-Fast Iterators Detect Changes
Collections such as ArrayList, HashMap, HashSet and LinkedList keep a field called modCount, which every structural modification increments. A structural modification is one that changes the size, such as add or remove; replacing a value with set() or with put() on an existing key is not one. When an iterator is created, it saves modCount as expectedModCount, and on each next() it compares the two.
Changes made through the iterator itself, with it.remove() or a ListIterator's add(), update the iterator's expectedModCount, so they are allowed. The point of the design is to stop at the first sign that the iterator's view of the collection is no longer valid, instead of carrying on and returning wrong results.
2Why the Check Is Only Best-Effort
The Javadoc is explicit that fail-fast behaviour cannot be guaranteed and that programs must not depend on it for correctness. There are two reasons. First, modCount is a plain int, not a volatile or synchronized field, so a change made by another thread may not be visible to the iterating thread at all. Second, the check only runs in next(), so a modification that happens just before hasNext() returns false is never noticed. Removing the second-to-last element of an ArrayList during a loop ends the loop quietly and skips the last element.
So the right way to think about ConcurrentModificationException is as a bug detector, like an assertion. When it fires, there is a real bug. When it does not fire, that proves nothing.
3Snapshot Iterators: CopyOnWriteArrayList
CopyOnWriteArrayList never modifies its internal array in place. Every write, whether add, set or remove, copies the array, changes the copy and then swaps the new array in through a volatile reference. An iterator simply holds a reference to whichever array was current when it was created.
So the iterator works on a snapshot: it never throws, it is never affected by later changes, and it never sees them either. Its remove() is not supported, because changing a snapshot would mean nothing. The cost is on the write side: every write copies the whole array, which suits lists that are read often and changed rarely, such as listener lists.
4Weakly Consistent Iterators: ConcurrentHashMap
ConcurrentHashMap does not copy anything. Its iterators walk the live table while other threads change it. The Javadoc calls them weakly consistent: they never throw ConcurrentModificationException, each element is returned at most once, and they reflect the state of the map at some point at or since the iterator's creation, which in practice means an update made during iteration may or may not be seen, depending on whether the iterator had already passed that bucket.
This gives the best concurrency of the three, since readers and writers do not block each other and nothing is copied, but it gives no point-in-time view. A total computed by iterating a ConcurrentHashMap while transfers update it is not the total at any single moment. Iterator.remove() is supported and removes the entry from the live map.
| Iterator | Concurrent change | Sees later changes | Cost |
|---|---|---|---|
Fail-fast (ArrayList, HashMap) | Usually throws CME, not guaranteed | Not applicable: it stops | None extra |
Snapshot (CopyOnWriteArrayList) | Never throws | Never | Array copy on every write |
Weakly consistent (ConcurrentHashMap) | Never throws | Maybe, no point in time | None extra |
5A Note on the Term Fail-Safe
Many tutorials call the second and third kinds fail-safe iterators. The term is informal: it does not appear in the Javadoc, which uses snapshot for CopyOnWriteArrayList and weakly consistent for the concurrent collections. The informal name is also misleading, because it suggests the iterator is safe in every sense. Neither kind fails, but they give very different guarantees about what we see, and in an interview it is worth naming them precisely.
6Follow-up questions
Does Collections.synchronizedList give fail-safe iteration?
No. Its iterator is the underlying list's fail-fast iterator, and the wrapper does not lock across iteration. The Javadoc requires us to hold the list's lock with synchronized (list) around the whole loop. Only its forEach and removeIf run under the lock automatically.
Is ConcurrentHashMap's size() also weakly consistent?
In effect, yes. It sums a base counter and striped counter cells without stopping writers, so the result can be out of date while updates are happening. For large maps mappingCount(), which returns a long, is preferred, with the same caveat.
Why does HashMap allow set-like changes such as replacing a value during iteration?
Because replacing the value of an existing key does not change the table's structure, so the iterator's position is still valid. The fail-fast check is about the iterator's bookkeeping, not about values changing.
7Conclusion
In this answer, we saw that fail-fast iterators in ArrayList and HashMap detect structural changes made outside the iterator through modCount and throw ConcurrentModificationException, but only on a best-effort basis, so the exception is a bug detector rather than a guarantee. CopyOnWriteArrayList iterators walk a snapshot array, never throw and never see later changes, at the cost of copying on every write. ConcurrentHashMap iterators are weakly consistent, never throw and may or may not show concurrent updates. Fail-safe is an informal name for these last two, and the Javadoc's precise terms are snapshot and weakly consistent.
Open with
Fail-fast iterators, like the ones from ArrayList and HashMap, protect against using an iterator after the collection has been structurally changed by someone else. The collection keeps a modCount that every add or remove increments, the iterator records it when created, and next compares the two and throws ConcurrentModificationException if they differ.
Then, if they want more
Changes through the iterator itself, like it.remove, are fine. The important nuance is that it's best-effort: modCount isn't volatile, so another thread's change may not be visible, and the check is only in next, so some modifications slip through, like removing the second-to-last element. So the Javadoc says not to rely on it for correctness; it's a bug detector.
CopyOnWriteArrayList takes a different approach. Every write copies the array and swaps it in, and the iterator holds the array that was current when it started, so it works on a snapshot. It never throws, never sees later changes, and doesn't support remove. That's great for read-mostly data like listener lists, but every write costs a full copy.
ConcurrentHashMap's iterators are weakly consistent: they walk the live table, never throw, return each element at most once, and may or may not reflect updates made during iteration. You get great concurrency but no point-in-time view.
People often call those last two fail-safe, but that's an informal term; the Javadoc says snapshot and weakly consistent, and I'd use those because they say what you actually get.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain fail-fast iterators and the alternative behaviour of iterators from...": https://scrivio-interview-library.netlify.app/java/iteration/a573ef3c67/