Back to Blog
Developer guide
JAVAMay 8, 2026

Java ConcurrentModificationException Explained: Causes, Fixes, and Prevention

Java ConcurrentModificationException is a common error that occurs when a program attempts to modify a collection (such as a list, set, or map) while another thread is iterating over it. This error can be frustrating, especially when debugging complex multi-threaded applications. In this article, we'll delve into the causes of Java ConcurrentModificationException, provide practical solutions to fix it, and explore ways to prevent it from occurring in the first place.

1. ConcurrentModificationException in Java

ConcurrentModificationException is thrown when a program attempts to modify a collection while another thread is iterating over it. This error typically occurs when using methods like Iterator.remove(), listIterator.remove(), or set.remove() while iterating over a collection.

Why It Happens

ConcurrentModificationException occurs due to the lack of proper synchronization between threads when accessing shared collections. This can happen when multiple threads are iterating over the same collection and attempting to modify it simultaneously.

How to Fix It

To fix ConcurrentModificationException, you can use synchronization techniques, such as using a ReentrantLock or a synchronized block, to ensure that only one thread can access and modify the collection at a time.


2. Using Iterator.remove() without Iterator's Removal Policy

Iterator.remove() can throw ConcurrentModificationException if the iterator is not properly synchronized. This can happen when using Iterator.remove() in a multi-threaded environment without ensuring that the iterator is thread-safe.

Why It Happens

Iterator.remove() throws ConcurrentModificationException when the iterator is not synchronized, leading to unexpected behavior and errors.

How to Fix It

To avoid this issue, use the Iterator's own removal policy by calling the Iterator's remove() method and ensuring that the iterator is properly synchronized. Alternatively, use a loop to iterate over the collection and remove elements manually.


3. Modifying a Collection while Enumerating it using an Enumeration

Modifying a collection while enumerating it using an Enumeration can throw ConcurrentModificationException. This is because Enumerations are not thread-safe and can lead to unexpected behavior in multi-threaded environments.

Why It Happens

Enumerations are not designed to handle concurrent access and modification of collections, making them prone to ConcurrentModificationException.

How to Fix It

To avoid this issue, use an Iterator instead of an Enumeration, and ensure that the Iterator is properly synchronized. Alternatively, use a loop to iterate over the collection and remove elements manually.


4. Using a Thread-Safe Collection

Using a thread-safe collection, such as CopyOnWriteArrayList or Collections.synchronizedList(), can prevent ConcurrentModificationException in multi-threaded environments.

Why It Happens

ConcurrentModificationException occurs when using non-thread-safe collections in multi-threaded environments, leading to unexpected behavior and errors.

How to Fix It

Use a thread-safe collection, such as CopyOnWriteArrayList or Collections.synchronizedList(), to ensure that collections are accessed and modified safely in multi-threaded environments.


5. Avoiding ConcurrentModificationException in Java Streams

ConcurrentModificationException can occur when using Java streams and attempting to modify a collection while iterating over it.

Why It Happens

Java streams can throw ConcurrentModificationException when modifying a collection while iterating over it, leading to unexpected behavior and errors.

How to Fix It

To avoid this issue, use the stream's built-in methods, such as filter() or map(), to process the collection without modifying it. Alternatively, use a loop to iterate over the collection and remove elements manually.


6. ConcurrentModificationException with Java's ListIterator

ConcurrentModificationException can occur when using Java's ListIterator and attempting to remove elements while iterating over a list.

Why It Happens

ListIterator's remove() method can throw ConcurrentModificationException if the iterator is not properly synchronized, leading to unexpected behavior and errors.

How to Fix It

To avoid this issue, use the ListIterator's own removal policy by calling the ListIterator's remove() method and ensuring that the iterator is properly synchronized. Alternatively, use a loop to iterate over the collection and remove elements manually.


7. ConcurrentModificationException with Java's Array

ConcurrentModificationException can occur when modifying an array while iterating over it.

Why It Happens

Modifying an array while iterating over it can lead to ConcurrentModificationException, especially in multi-threaded environments.

How to Fix It

To avoid this issue, use a thread-safe collection, such as CopyOnWriteArrayList or Collections.synchronizedList(), instead of an array. Alternatively, use a loop to iterate over the collection and remove elements manually.


8. ConcurrentModificationException with Java's HashMap

ConcurrentModificationException can occur when modifying a HashMap while iterating over its entries.

Why It Happens

Modifying a HashMap while iterating over its entries can lead to ConcurrentModificationException, especially in multi-threaded environments.

How to Fix It

To avoid this issue, use a thread-safe collection, such as ConcurrentHashMap, instead of a HashMap. Alternatively, use a loop to iterate over the collection and remove elements manually.

Conclusion

ConcurrentModificationException is a common error that occurs in Java when a program attempts to modify a collection while another thread is iterating over it. By understanding the causes of this error and implementing the solutions outlined in this article, developers can prevent ConcurrentModificationException and write more robust, thread-safe applications.

Explore More Debugging Resources

- [Browse all JAVA errors](/languages/java)

- [Browse errors by type](/error-types)

- [Search all documented errors](/search)

- [Use the Error Explainer](/error-explainer-tool)

Browse allJava errors

Related JAVA Articles

Have a specific error? Get an instant explanation.

Explain an Error