Loop to avoid java.util.ConcurrentModificationException

In this article, we will learn about the Loop to avoid java.util.ConcurrentModificationException. When you are in a single-threaded environment and you want your code to support additional objects added to the list. You can do this using a for loop instead of a loop.

for(int i = 0; i<myList.size(); i++){
	System.out.println(myList.get(i));
	if(myList.get(i).equals("3")){
		myList.remove(i);
		i--;
		myList.add("6");
	}
}

Therefore notice that I decrease the counter because I am deleting the same object and If you want to delete the next item or item, there is no need to reduce the number of counters.

Therefore one more thing is that you get ConcurrentModificationException. If you are trying to change the original list structure using sublists, let’s look at a simple example.

Example

package com.HandyOpinion.Concurrent_Modification_Exception;

import java.util.ArrayList;
import java.util.List;

public class Concurrent_Modification_ExceptionWithArrayListSubList {

	public static void main(String[] args) {

		List<String> names = new ArrayList<>();
		names.add("Java");
		names.add("PHP");
		names.add("SQL");
		names.add("Angular 2");

		List<String> first2Names = names.subList(0, 2);

		System.out.println(names + " , " + first2Names);

		names.set(1, "JavaScript");
		// check the output below. :)
		System.out.println(names + " , " + first2Names);

		// Let's modify the list size and get ConcurrentModificationException
		names.add("Node_JS");
		System.out.println(names + " , " + first2Names); // this line throws exception

	}

}

Output

[Java, PHP, SQL, Angular 2] , [Java, PHP]
[Java, JavaScript, SQL, Angular 2] , [Java, JavaScript]
Exception in thread "main" java.util.ConcurrentModificationException
	at java.base/java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1282)
	at java.base/java.util.ArrayList$SubList.listIterator(ArrayList.java:1151)
	at java.base/java.util.AbstractList.listIterator(AbstractList.java:311)
	at java.base/java.util.ArrayList$SubList.iterator(ArrayList.java:1147)
	at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:465)
	at java.base/java.lang.String.valueOf(String.java:2801)
	at java.base/java.lang.StringBuilder.append(StringBuilder.java:135)
	at com.HandyOpinion.ConcurrentModificationException.ConcurrentModificationExceptionWithArrayListSubList.main(ConcurrentModificationExceptionWithArrayListSubList.java:26)

According to the documentation for an ArrayList sublist, structural changes are only allowed in the list returned by all of the sublist methods in the returned list. It first checks whether the actual value of modCount in the fallback corresponds to the expected value and throws a ConcurrentModificationException if it does not.

That’s all about how to use the Loop to avoid java.util.ConcurrentModificationException.

Next Article

Avoid ConcurrentModificationException in multi-threaded

Please share this post:
Posts created 55

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: