Back to Blog
Developer guide
JAVAJune 18, 2026

Java ConcurrentModificationException Explained: Causes, Solutions, and Best Practices

As Java developers, we often encounter errors that can be frustrating and challenging to resolve. One such error is the ConcurrentModificationException, which can occur when multiple threads try to access or modify a shared collection simultaneously. In this article, we'll delve into the causes of ConcurrentModificationException, provide actionable solutions, and offer best practices to help you write more robust and thread-safe Java code.

1. ConcurrentModificationException

ConcurrentModificationException occurs when a program attempts to modify a collection while it's being iterated over by another thread.

Why It Happens

This exception is typically caused by the unsynchronized access of a shared collection from multiple threads. When one thread tries to modify the collection while another thread is iterating over it, the JVM throws a ConcurrentModificationException.

How to Fix It

To avoid ConcurrentModificationException, you can use the Iterator's remove() method to safely remove elements from the collection while iterating. Alternatively, you can synchronize access to the collection using the Collections.synchronizedList() or Collections.synchronizedSet() methods.


2. Iterator's Fail-Fast Behavior

The Iterator's fail-fast behavior causes the ConcurrentModificationException when it detects that the collection has been modified during iteration.

Why It Happens

The fail-fast behavior is the default behavior of Java iterators. When the iterator detects a modification to the collection, it throws a ConcurrentModificationException.

How to Fix It

To avoid the fail-fast behavior, you can use the Iterator's remove() method or create a copy of the collection before iterating over it. Additionally, you can use the ListIterator's set() method to safely modify the collection during iteration.


3. HashMap and ConcurrentHashMap

HashMap and ConcurrentHashMap are not immune to ConcurrentModificationException, especially when iterating over their entrySet() or keySet().

Why It Happens

When iterating over a HashMap or ConcurrentHashMap, you may encounter ConcurrentModificationException if multiple threads try to modify the map while iterating over its keys or values.

How to Fix It

To avoid ConcurrentModificationException when iterating over a HashMap or ConcurrentHashMap, use the entrySet().iterator().forEachRemaining() method to safely iterate over the map's entries. Alternatively, use the ConcurrentHashMap's compute() or merge() methods to safely modify the map during iteration.


4. List and ArrayList

Lists and ArrayLists can also throw ConcurrentModificationException when iterating over them and modifying their elements simultaneously.

Why It Happens

When iterating over a List or ArrayList and modifying its elements at the same time, the ConcurrentModificationException occurs due to the unsynchronized access of the shared collection.

How to Fix It

To avoid ConcurrentModificationException when iterating over a List or ArrayList, use the ListIterator's set() method to safely modify the list during iteration. Alternatively, create a copy of the list before iterating over it or use the Collections.synchronizedList() method to synchronize access to the list.


5. Set and HashSet

Sets and HashSets can throw ConcurrentModificationException when iterating over them and adding or removing elements simultaneously.

Why It Happens

When iterating over a Set or HashSet and modifying its elements at the same time, the ConcurrentModificationException occurs due to the unsynchronized access of the shared collection.

How to Fix It

To avoid ConcurrentModificationException when iterating over a Set or HashSet, use the Iterator's remove() method to safely remove elements from the set during iteration. Alternatively, create a copy of the set before iterating over it or use the Collections.synchronizedSet() method to synchronize access to the set.


6. CopyOnWriteArrayList

CopyOnWriteArrayList is designed to prevent ConcurrentModificationException by creating a copy of the list on write operations.

Why It Happens

CopyOnWriteArrayList is designed to handle concurrent modifications, but it can still throw exceptions if not used correctly.

How to Fix It

To use CopyOnWriteArrayList safely, ensure that you're iterating over the list using the listIterator() method and avoid modifying the list during iteration. Additionally, use the CopyOnWriteArrayList's addIfAbsent() method to safely add elements to the list without modifying it during iteration.


7. Thread-Safe Collections

Utilize thread-safe collections like CopyOnWriteArrayList, ConcurrentHashMap, and CopyOnWriteArraySet to avoid ConcurrentModificationException.

Why It Happens

Thread-safe collections are designed to handle concurrent modifications, but they can still throw exceptions if not used correctly.

How to Fix It

Use thread-safe collections like CopyOnWriteArrayList, ConcurrentHashMap, and CopyOnWriteArraySet to safely iterate over and modify collections concurrently. Additionally, ensure that you're using the correct methods to modify and iterate over these collections.

Conclusion

In conclusion, ConcurrentModificationException is a common error that can occur in multi-threaded Java applications. By understanding the causes, solutions, and best practices outlined in this article, you can write more robust and thread-safe code that avoids this exception. Remember to use the Iterator's remove() method, Collections.synchronizedList(), and thread-safe collections like CopyOnWriteArrayList and ConcurrentHashMap to safely iterate over and modify collections concurrently.

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