Fundamentals 5 min read

Understanding Fail-Fast Iterators and ConcurrentModificationException in Java Collections

The article explains why fail‑fast iterators throw ConcurrentModificationException when a collection is modified during iteration, outlines two common scenarios that trigger the exception, and presents both incorrect and correct Java code examples along with strategies such as avoiding modifications, using synchronization, iterator.remove(), or switching to fail‑safe concurrent collections.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Understanding Fail-Fast Iterators and ConcurrentModificationException in Java Collections

When the underlying collection is modified during iteration, fail‑fast iterators may throw a ConcurrentModificationException , quickly failing instead of allowing nondeterministic behavior later.

The exception mainly occurs in two situations: (1) the collection is altered via its own methods while a fail‑fast iterator is iterating it in the same thread, and (2) one thread modifies the collection’s structure while another thread is iterating it with a fail‑fast iterator.

Example that triggers the exception (modifying the list inside the loop):

package com.renzhikeji.demo;

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

/**
 * @author 认知科技技术团队
 * 微信公众号:认知科技技术团队
 */
public class JdkDemo {
    public static void main(String[] args) throws InterruptedException {
        List
list = new ArrayList<>(List.of(1, 2, 3, 5, 6, 7, 8));
        Iterator
iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer a = iterator.next();
            System.out.println(a);
            list.add(10); // modifies the collection during iteration
        }
    }
}

Another erroneous example where the collection’s remove method is called directly inside the loop:

package com.renzhikeji.demo;

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

/**
 * @author 认知科技技术团队
 * 微信公众号:认知科技技术团队
 */
public class JdkDemo {
    public static void main(String[] args) throws InterruptedException {
        List
list = new ArrayList<>(List.of(1, 2, 3, 5, 6, 7, 8));
        Iterator
iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer a = iterator.next();
            System.out.println(a);
            list.remove((Object)7); // direct removal causes exception
        }
    }
}

Correct approach using the iterator’s remove() method after a condition check:

package com.renzhikeji.demo;

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

/**
 * @author 认知科技技术团队
 * 微信公众号:认知科技技术团队
 */
public class JdkDemo {
    public static void main(String[] args) throws InterruptedException {
        List
list = new ArrayList<>(List.of(1, 2, 3, 5, 6, 7, 8));
        Iterator
iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer a = iterator.next();
            System.out.println(a);
            if (a == 7) {
                iterator.remove(); // safe removal via iterator
            }
        }
        System.out.println(list);
    }
}

To handle concurrent modifications, two strategies are recommended: (1) avoid modifying the collection while iterating—use synchronization to prevent concurrent access or always use iterator.remove() instead of Collection.remove() ; (2) replace fail‑fast iterators with fail‑safe ones from concurrent collections such as ConcurrentHashMap or CopyOnWriteArrayList , which do not throw this exception.

JavaConcurrencyThread SafetyCollectionsConcurrentModificationExceptionFail-Fast Iterator
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.