ConcurrentModificationException in JavaLast Updated : 30 Jan 2026 ConcurrentModificationException in Java is an exception that tells us that the collection is modified structurally while any of its elements are being traversed concurrently. This generally occurs when the collection is changed during an iteration of an iterator (for example, adding or removing elements). Let's delve into this in more detail: 1. Concurrency and Iteration In Java, collections may be modified concurrently so that one thread is modifying the collection while another is traversing it. Yet, the Java Collections Framework, by default, is not inherently thread-safe. By that, it means if you try to modify a collection while another thread is iterating over it, you will encounter such kind of problems as ConcurrentModificationException. 2. Cause of ConcurrentModificationException The most usual case of ConcurrentModificationException occurs when one uses an iterator (e.g., Iterator, ListIterator) to iterate over a collection while modifying the collection (i.e., adding/removing elements) outside the iterator context. The iterator takes care of the internal structure of the collection. It throws a ConcurrentModificationException if it detects that the structure has been changed outside of itself, which implies that the current iteration is not valid. Example of ConcurrentModificationExceptionNote: It is not mandatory that this exception will be thrown only when some other thread tries to modify a Collection object. It can also happen if a single thread has some methods called which are trying to violate the contract of the object. It may happen when a thread is trying to modify the Collection object while it is being iterated by some fail-fast iterator; the iterator will throw the exception.ExampleOutput: ![]() This message says that the exception is thrown when the next method is called as the iterator is iterating the list and we are making modifications in it simultaneously. But if we make modifications in hashmap like given below, then it will not throw any such exception as the size of the hashmap won't change. For Example- Output: Map Value:1 Map Value:2 Map Value:3 This example works completely fine as while the iterator is iterating over the map, the size of the map is not changing. Only the map is being updated in the if statement. Constructors of ConcurrentModificationExceptionThere are 4 types of constructors of ConcurrentModificationException -
How to avoid ConcurrentModificationException in a multi-threaded environment?To avoid ConcurrentModificationException in a multiprogramming environment, we need to employ techniques to synchronize the access to those shared collections or use thread-safe data structures. Here are some techniques to accomplish this: 1. Use Thread-Safe Collections: Java has several thread-safe collection classes in the java.util.concurrent packages like ConcurrentHashMap, CopyOnWriteArrayList, ConcurrentLinkedQueue, etc which are created to be used safely in a multi-threading environment without doing external synchronization. Example: 2. Synchronize Access: If you are using the regular collections (for example, ArrayList, HashMap, etc.), synchronization of their access has to be done manually using explicit blocking/synchronization (for example, using synchronized keyword or ReadWriteLock). Example: 3. Iterator's remove() method: While iterating through the collection, use the iterator's remove() method instead of modifying directly on the collection. This ensures that all adjustments are made safely through the iterator. Example 4. Copy the Collection: If the size of the collection is small and iterations over it are made from time to time, you should consider making a copy of it before modifying it. Hence, you can refactor the copy safely without checking for concurrent modifications. 5. Use Immutable Collections: If the collection is necessary to avoid modification after creation, consider immutable collections from libraries like Google Guava or Java 9+ List.of(), Set.of(), etc. Immutable collections can be safely shared among threads without synchronization. Example : 6. Atomic Operations: For specialized instances, you can use atomic operations, which are offered by classes like AtomicInteger, AtomicLong, etc., to achieve thread safe modification. Example: 7. Proper Thread Synchronization: Proper thread synchronization can avoid concurrent modification issues. Synchronization constructs such as synchronized blocks, Lock objects, etc., can be employed to control access to shared data. ConcurrentModificationExample.java Output: Added: Element 0 Added: Element 1 Added: Element 2 Added: Element 3 Added: Element 4 4.2 How to avoid ConcurrentModificationException in a single-threaded environment?In a single-threaded environment, you typically don't face the same concurrency issues as in a multi-threaded environment. However, you might still encounter ConcurrentModificationException if you attempt to modify a collection while iterating over it. Here's how you can avoid it in a single-threaded context: 1. Use Iterator's remove() Method: 2. Use for-each Loop: In Java 5 and later, you can use the enhanced for loop, which internally uses an iterator and allows the safe removal of elements: 3. Use ListIterator: If you need to traverse the list bidirectionally and modify it, you can use ListIterator: 4. Copy the Collection: If you need to modify the collection while iterating over it, consider making a copy of the collection and iterating over the copy. This way, modifications won't affect the original collection: 5. Use Stream API: With Java 8 and later, you can use the Stream API to manipulate collections safely: AvoidConcurrentModification.java Output: After removing 'B' with Iterator: [A, C] After removing 'C' with for-each loop: [A] After removing 'A' with ListIterator: [] After removing 'B' by copying: [] After removing 'A' with Stream API: [] 5. Debugging ConcurrentModificationException When such a problem occurs, you have to analyze your code to find out from which part of your code modification of the collection happens concurrently with the iteration. Once you have identified the problem, you can pick one of the strategies mentioned above to fix it. ConcurrentModificationExample.java Output: Processing element: A Processing element: B Modifying the list while iterating... Modified list: [A, C] Processing element: C Next TopicJava Tutorial |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
