Browse Java Design Patterns & Enterprise Application Architecture

Fail-Fast and Fail-Safe Iterators

Understand Java fail-fast and fail-safe iterator behavior so mutation and traversal expectations stay realistic.

Iterator design is not only about order. It is also about mutation semantics.

Fail-Fast

Many Java collection iterators are fail-fast. If the underlying collection changes unexpectedly during iteration, the iterator may throw ConcurrentModificationException.

That is a debugging aid, not a thread-safety guarantee.

Fail-Safe Or Snapshot-Like Behavior

Some iterator designs work over copies or concurrent views and continue despite structural changes. That changes performance and visibility semantics.

Review Rule

If you design a custom iterator, document mutation behavior clearly. “What happens if the collection changes during traversal?” is part of the iterator contract.

Revised on Thursday, April 23, 2026