liveBook · Manning

ConcurrentModificationException

As explained in 100 Java Mistakes and How to Avoid Them

ConcurrentModificationException is the Java runtime exception that signals an unsupported structural modification detected during iteration or another collection operation.

ConcurrentModificationException signals an unsupported structural change during iteration. Enhanced for-each syntax uses an iterator, so adding or removing directly from a non-concurrent collection can fail fast on the next iterator operation. section 8.8

List<String> list = new ArrayList<>(List.of("a", "b", "c"));
for (String s : list) {
  System.out.println(s);
  if (s.equals("b")) {
    list.add("x");
  }
}

This minimal example adds to a list inside enhanced for-each iteration, exposing the faulty pattern. section 8.8

Standard collections track a modCount value. An iterator copies it when created; next() compares the copy with the collection’s current value and throws when they differ. This fail-fast check avoids continuing with uncertain visibility of later changes. section 8.8

Avoid the mistake with these practical alternatives:

  • Modify through the iterator used for iteration when the operation is supported. section 8.8
  • Use an indexed loop when direct list modification is intentional, while recognizing that classic loops are fragile. section 8.8
  • Iterate over a copy when the original collection must be modified during processing. section 8.8

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains fail-fast detection through modCount and next(), while distinguishing iterator, enhanced for-each, and indexed-loop behavior.
Examples
Uses ArrayList examples that add or remove elements during enhanced for-each and iterator loops, plus indexed-loop and copy alternatives.
What it assumes
Assumes Java collection iteration and relates enhanced for-each syntax to iterator loops.
What you can do after
Diagnose/debug iterator failures and choose iterator, indexed-loop, or collection-copy strategies for intentional modification.
How it connects
Connects ConcurrentModificationException with iterators, enhanced for-each loops, indexed loops, modCount, and fail-fast behavior.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.