ProLesson 7.8Intermediate3 min readUpdated 30 October 2026
Iterators, fail-fast and fail-safe
How for-each really works, what Iterator and ListIterator do, and why some collections throw ConcurrentModificationException while others don't.
Every time you write a for-each loop over a collection, Java quietly uses an Iterator. Understanding it explains a famous exception and a classic interview question: fail-fast vs fail-safe.
01What for-each really does
java
for (String s : list) { ... }is compiled into:
java
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
...
}Any class that implements Iterable can be used in a for-each loop.
Loading the rest of the lesson…