Java Reference ยท java.lang
The ConcurrentModificationException class
Reference for ConcurrentModificationException: why changing a collection inside a for-each loop fails, fail-fast iterators, and safe ways to remove items.
java.util.ConcurrentModificationException (CME) is an unchecked exception thrown by the fail-fast iterators of the standard collections when the collection is structurally modified while an iteration is in progress. Despite the name, it usually has nothing to do with threads: the most common cause is removing an element from an ArrayList inside a for-each loop over that same list.
A structural modification is one that changes the size of the collection (add, remove, clear). Replacing an element with set() on a list is not structural. Fail-fast iterators detect the problem with a modification counter and throw as a best-effort bug detector; the behavior is not guaranteed, so never write code that depends on catching the exception.
public class ConcurrentModificationException extends RuntimeException
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public ConcurrentModificationException() public ConcurrentModificationException(String message) public ConcurrentModificationException(Throwable cause) public ConcurrentModificationException(String message, Throwable cause)
| Constructor | Description |
|---|---|
() |
No message. The JDK iterators throw this form, so getMessage() is null. |
(String message) |
Detail message. |
(Throwable cause) |
Wraps a cause. |
(String message, Throwable cause) |
Message and cause. |
Usage notes
- Why the for-each loop fails: the enhanced
forloop uses the list's iterator behind the scenes.list.remove()bumps the list's modification counter, and the iterator notices the mismatch on the nextnext()call. - Safe ways to remove while looping: call
Iterator.remove(), useCollection.removeIf(predicate)(preferred; also faster forArrayListbecause it compacts once), or build a new collection with the elements you want to keep. - Adding while iterating is equally forbidden. Collect the additions in a second list and call
addAllafter the loop. - Maps: the iterators of
keySet(),values()andentrySet()are fail-fast too. Changing the value of an existing key (puton an existing key) is not structural and is allowed; adding or removing keys is not. - Threads: if another thread modifies a plain
ArrayListorHashMapwhile you iterate, you may get the exception, or silently wrong results, or neither. Fail-fast is not a thread-safety mechanism. Use concurrent collections or lock. - Weakly consistent iterators:
CopyOnWriteArrayListiterates over a snapshot andConcurrentHashMapiterators tolerate concurrent changes; neither throws this exception. - Streams: modifying the source of a stream pipeline during a terminal operation can throw it as well.
- Do not catch it as a fix. The exception reveals a logic error; change the code so the modification cannot happen mid-iteration.