Java — How to deal with ConcurrentModificationException in Java | Code Factory

Using for each loop and ArrayList.remove(index)

package com.example.java.programming;import java.util.ArrayList;
import java.util.List;
/**
* @author code.factory
*
*/
public class ArrayListExample {
public static void main(String... args) {
List<String> names = new ArrayList<>();
names.add("Narendra");
names.add("Amit");
names.add("Rahul");
names.add("Yogi");
System.out.println("List before : " + names);
/*
* Using forEach loop to iterate and removing element during iteration will
* throw ConcurrentModificationException in Java
*/
for (String name : names) {
if (name.contains("i")) {
names.remove(name);
}
}
System.out.println("List after : " + names);
}
}
List before : [Narendra, Amit, Rahul, Yogi]
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at com.example.java.programming.ArrayListExample.main(ArrayListExample.java:24)

Using classical for loop and ArrayList.remove(index)

package com.example.java.programming;import java.util.ArrayList;
import java.util.List;
/**
* @author code.factory
*
*/
public class ArrayListExample {
public static void main(String... args) {
List<String> names = new ArrayList<>();
names.add("Narendra");
names.add("Amit");
names.add("Rahul");
names.add("Yogi");
System.out.println("List before : " + names);
for (int i = 0; i < names.size(); i++) {
String name = names.get(i);
if (name.contains("i")) {
System.out.println("Removing " + name);
names.remove(i); // will throw CME
}
}
System.out.println("List after : " + names);
}
}
List before : [Narendra, Amit, Rahul, Yogi]
Removing Amit
Removing Yogi
List after : [Narendra, Rahul]
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}

Using Iterator but ArrayList’s remove method

package com.example.java.programming;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author code.factory
*
*/
public class ArrayListExample {
public static void main(String... args) {
List<String> names = new ArrayList<>();
names.add("Narendra");
names.add("Amit");
names.add("Rahul");
names.add("Yogi");
System.out.println("List before : " + names);
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String name = iterator.next();
names.remove(name);
}
System.out.println("List after : " + names);
}
}
List before : [Narendra, Amit, Rahul, Yogi]
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at com.example.java.programming.ArrayListExample.main(ArrayListExample.java:23)

Right way to remove element is by using Iterator’s remove method

package com.example.java.programming;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author code.factory
*
*/
public class ArrayListExample {
public static void main(String... args) {
List<String> names = new ArrayList<>();
names.add("Narendra");
names.add("Amit");
names.add("Rahul");
names.add("Yogi");
System.out.println("List before : " + names);
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String name = iterator.next();
System.out.println("Removing : " + name);
iterator.remove();
}
System.out.println("List after : " + names);
}
}
List before : [Narendra, Amit, Rahul, Yogi]
Removing : Narendra
Removing : Amit
Removing : Rahul
Removing : Yogi
List after : []

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store