0 votes
1.8k views
in Java by
How to avoid concurrentmodificationexception while iterating a collection ?

2 Answers

0 votes
by

For Avoiding ConcurrentModificationException in multi-threaded environment you can do following things:

  1. To convert list to an array and then iterate the array. This approach is usefull for small or medium size list but when the list is larger then it will affect the performance.
  2. Lock the list while iterating. It put into a synchronized block. This approach is not benefificial for multithreading.
  3. If you are using JDK1.5 or higher then you can use ConcurrentHashMap and CopyOnWriteArrayList classes.
0 votes
by

When you run the following example then you got ConcurrentModificationException:

Main File: (ConcurrentModificationException.java)

package test.com.collection;

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

public class ConcurrentModificationException {

    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<Employee>();
        Employee emp;
        emp = new Employee(1, "Rohit", "Agrawal", "EMP001", 54000D);
        employees.add(emp);
        emp = new Employee(2, "Pin", "Rai", "EMP002", 1400D);
        employees.add(emp);
        emp = new Employee(3, "Sohan", "Kumar", "EMP003", 5000D);
        employees.add(emp);
        emp = new Employee(4, "Suresh", "Dhull", "EMP004", 24700D);
        employees.add(emp);
        
        doModifyList(employees);
    }
    
    private static void doModifyList(List<Employee> employees) {
        int index = 0;
        for(Employee emp: employees){
            System.out.println(emp);
            if(emp.getFirstName().equals("Pin")){
                employees.add(index, new Employee(4, "SureshNew", "DhullNew", "EMP004", 24700D));
                employees.remove(emp);
            }
            index ++;
        }
    }

}

Bean class:- (Employee.java):- It has only setter and getter methods.


Output:
Id: 1 FirstName: Rohit LastName: Agrawal Code: EMP001 Salary: 54000.0
Exception in thread "main" Id: 2 FirstName: Pin LastName: Rai Code: EMP002 Salary: 1400.0
java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at test.com.collection.ConcurrentModificationException.doModifyList(ConcurrentModificationException.java:25)
    at test.com.collection.ConcurrentModificationException.main(ConcurrentModificationException.java:20)

TO avoid ConcurrentModificationException the you need to improve the following codes:

for(Employee emp: employees){
     System.out.println(emp);
     if(emp.getFirstName().equals("Pin")){
        employees.add(index, new Employee(4, "SureshNew", "DhullNew", "EMP004", 24700D));
        employees.remove(emp);
     }
     index ++;
}

For avoiding ConcurrentModificationException you will use (Iterator in place of forEach) the following codes:

System.out.println("Before removing: " + employees);
for(final Iterator<Employee> iterator = employees.iterator(); iterator.hasNext();){
    Employee emp = iterator.next();
    if(emp.getFirstName().equals("Pin")){
        iterator.remove();
    }
    index ++;
}
System.out.println("After removing: " + employees);

Output:
Before removing: [Id: 1 FirstName: Rohit LastName: Agrawal Code: EMP001 Salary: 54000.0, Id: 2 FirstName: Pin LastName: Rai Code: EMP002 Salary: 1400.0, Id: 3 FirstName: Sohan LastName: Kumar Code: EMP003 Salary: 5000.0, Id: 4 FirstName: Suresh LastName: Dhull Code: EMP004 Salary: 24700.0]
After removing: [Id: 1 FirstName: Rohit LastName: Agrawal Code: EMP001 Salary: 54000.0, Id: 3 FirstName: Sohan LastName: Kumar Code: EMP003 Salary: 5000.0, Id: 4 FirstName: Suresh LastName: Dhull Code: EMP004 Salary: 24700.0]

}

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...