Back to Blog
Developer guide
JAVAJune 20, 2026

Java ConcurrentModificationException: Causes, Solutions, and Best Practices

ConcurrentModificationException is a common issue in Java programming that occurs when a program attempts to modify a collection (like a list or set) while it is being iterated over by a foreach loop, iterator, or other concurrent operation. In this article, we'll delve into the causes of ConcurrentModificationException, provide actionable solutions, and offer best practices for avoiding it in your Java code. Whether you're a seasoned Java developer or just starting out, this guide will help you tackle ConcurrentModificationException and write more robust, concurrent code.

1. ConcurrentModificationException

ConcurrentModificationException occurs when a program attempts to modify a collection while it is being iterated over by a foreach loop, iterator, or other concurrent operation.

Why It Happens

The most common causes of ConcurrentModificationException include using a foreach loop to iterate over a collection while modifying it using a separate thread or another loop, or using an iterator to modify a collection.

How to Fix It

To avoid ConcurrentModificationException, use a thread-safe collection like CopyOnWriteArrayList or ConcurrentHashMap, or use a CopyOnWriteIterator to iterate over a collection while modifying it.


2. Iterator.remove() vs Iterator.next()

Iterator.remove() and Iterator.next() operations can throw ConcurrentModificationException if they are executed in the wrong order.

Why It Happens

Iterator.remove() and Iterator.next() operations are not thread-safe, and executing them in the wrong order can cause ConcurrentModificationException.

How to Fix It

To avoid ConcurrentModificationException, always call Iterator.next() before Iterator.remove(), and make sure to remove the correct element.


3. ArrayList vs Vector

Using ArrayList instead of Vector can cause ConcurrentModificationException when iterating over a collection.

Why It Happens

ArrayList is not thread-safe, and iterating over it while modifying it can cause ConcurrentModificationException.

How to Fix It

To avoid ConcurrentModificationException, use Vector instead of ArrayList when iterating over a collection, or use a thread-safe collection like CopyOnWriteArrayList.


4. foreach Loop vs Iterator

Using a foreach loop instead of an iterator can sometimes avoid ConcurrentModificationException.

Why It Happens

foreach loops are not as prone to ConcurrentModificationException as iterators, but they are not guaranteed to be thread-safe.

How to Fix It

To avoid ConcurrentModificationException, use a foreach loop instead of an iterator, but be aware that it may not be thread-safe in all cases.


5. ListIterator vs Iterator

ListIterator is more prone to ConcurrentModificationException than Iterator.

Why It Happens

ListIterator is more susceptible to ConcurrentModificationException than Iterator because it supports bidirectional traversal and element insertion.

How to Fix It

To avoid ConcurrentModificationException, use Iterator instead of ListIterator when iterating over a list.


6. ConcurrentHashMap vs HashMap

Using ConcurrentHashMap instead of HashMap can avoid ConcurrentModificationException in multithreaded environments.

Why It Happens

HashMap is not thread-safe, and iterating over it while modifying it can cause ConcurrentModificationException in multithreaded environments.

How to Fix It

To avoid ConcurrentModificationException, use ConcurrentHashMap instead of HashMap in multithreaded environments.


7. CopyOnWriteArrayList vs ArrayList

Using CopyOnWriteArrayList instead of ArrayList can avoid ConcurrentModificationException in multithreaded environments.

Why It Happens

ArrayList is not thread-safe, and iterating over it while modifying it can cause ConcurrentModificationException in multithreaded environments.

How to Fix It

To avoid ConcurrentModificationException, use CopyOnWriteArrayList instead of ArrayList in multithreaded environments.


8. Thread-Safe vs Thread-Unsafe

Understanding the difference between thread-safe and thread-unsafe collections is crucial to avoiding ConcurrentModificationException.

Why It Happens

Thread-safe collections are designed to handle concurrent access, while thread-unsafe collections are not.

How to Fix It

To avoid ConcurrentModificationException, use thread-safe collections like ConcurrentHashMap, CopyOnWriteArrayList, or Vector, and avoid thread-unsafe collections like ArrayList or HashMap.

Conclusion

ConcurrentModificationException is a common issue in Java programming that can be avoided by using thread-safe collections, following best practices, and understanding the differences between thread-safe and thread-unsafe collections. By following the solutions and best practices outlined in this article, you can write more robust, concurrent code and avoid the frustration of ConcurrentModificationException.

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