Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Apr 22, 2023
Contribute to Docs

The ConcurrentModificationException exception is thrown when one thread is iterating through a collection using an Iterator object, and another thread modifies the collection by adding, removing, or modifying its elements. When the Iterator object tries to access the next element in the collection, it detects that the collection has been modified, and throws the ConcurrentModificationException.


The following example throws the ConcurrentModificationException because the arr ArrayList is modified during iteration:

import java.util.ArrayList;
public class ModificationError {
public static void main(String args[])
// Creating an object of ArrayList Object
ArrayList<String> arr = new ArrayList<String>();
try {
// Enhanced for loop, It uses the Iterator class under the hood.
for (String elem : arr) {
if (elem.equals("One")) {
arr.remove(elem); // This will throw the exception.
System.out.println(elem); // Will not reach this line.
catch (Exception e) {

All contributors

Looking to contribute?

Learn Java on Codecademy