java.util.ConcurrentModificationException is a very familiar exception when working with Java collection classes. Classes in the Java collection are fast, which means that will throw ConcurrentModificationException.

ConcurrentModificationExceptions can occur in a multi-threaded and single-threaded Java programming environment.


Following is the ConcurrentModificationException scenario with an example.

package com.HandyOpinion.Concurrent_Modification_Exception;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Concurrent_Modification_Exception_Example {

	public static void main(String args[]) {
		List<String> myList = new ArrayList<String>();


		Iterator<String> it = myList.iterator();
		while (it.hasNext()) {
			String value =;
			System.out.println("List Value:" + value);
			if (value.equals("3"))

		Map<String, String> myMap = new HashMap<String, String>();
		myMap.put("1", "1");
		myMap.put("2", "2");
		myMap.put("3", "3");

		Iterator<String> it1 = myMap.keySet().iterator();
		while (it1.hasNext()) {
			String key =;
			System.out.println("Map Value:" + myMap.get(key));
			if (key.equals("2")) {
				myMap.put("1", "4");
				// myMap.put("4", "4");


The above code will throw exception java.util.ConcurrentModificationException when the code executes as shown in the below console logs.

List Value:1
List Value:2
List Value:3
Exception in thread "main" java.util.ConcurrentModificationException
	at java.base/java.util.ArrayList$Itr.checkForComodification(
	at java.base/java.util.ArrayList$
	at com.HandyOpinion.ConcurrentModificationException.ConcurrentModificationExceptionExample.main(

From the output stack trace, it is clear that the simultaneous change exception is thrown when we call the iterator function next().

Implementation is in the AbstractList class, which has an int modCount variable define. The modCount indicates how often the size of the list has changed. The modCount value is used each time next() is called to look for changes in a checkForComodificatio() function.

Now comment out the part of the list and run the program again. You will see that no ConcurrentModificationException is thrown now.


Map Value:3
Map Value:2
Map Value:4

Since we are updating the existing key-value in myMap, its size has not changed and we do not get a ConcurrentModificationException. The output may be different in your system because the HashMap key set is not in order as a list.

If you comment out the statement we were adding a new key-value in the HashMap, a ConcurrentModificationException is thrown.

Next Article

1. To Avoid ConcurrentModificationException in a multi thread environment
2. Loop to avoid java.util.ConcurrentModificationException

Please share this post:
Posts created 58

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: