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 : []

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Failures and success of product development — datarockets

Ethical hacking with WebGoat

How to Build the Perfect RPA Centre of Excellence in 2020?

Big software news from Greyp

A Step-By-Step Guide To Convert Your Site From HTTP To HTTPS

I Created a Lorem Ipsum Generator for Donut Enthusiasts (Part 1)

AWS Solutions Architect Job Interview Service based Questions

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
Code Factory

Code Factory

More from Medium

Responsibility Driven Object Oriented Design

Number of Paths — Pramp question Java

Immutable behavior of Java String

Exception Handling in Java