Short answer
for-each desugars to an Iterator, and most collections track a modCount; structurally modifying the collection (add/remove) directly while iterating makes the iterator's expected count diverge, so the next call throws ConcurrentModificationException fail-fast.
Explanation
To remove during iteration, use the explicit Iterator.remove(), or Collection.removeIf(predicate) which handles it internally. Note this is a best-effort safety check, not a thread-safety guarantee.
Common mistakes
- Calling list.remove() inside a for-each loop.
- Not knowing Iterator.remove() or removeIf() are the safe ways.