How to Resolve java.util.ConcurrentModificationException on Collections in Java?

Question

What causes java.util.ConcurrentModificationException when modifying collections in Java?

List<String> myList = new ArrayList<>();
myList.add("A");
myList.add("B");

for (String item : myList) {
    myList.remove(item); // This line will throw ConcurrentModificationException
}

Answer

The java.util.ConcurrentModificationException is a runtime exception that occurs when one thread attempts to modify a collection while another thread is iterating over it, or when a collection is structurally modified during iteration in a single-threaded context. Understanding how to manage collection modifications is essential for smooth application functionality.

// Using Iterator for safe removal
yourList.removeIf(item -> item.equals("B")); // Prefer this instead of modifying while iterating

Causes

  • Modifying a collection (e.g., adding or removing elements) while iterating over it using an iterator or enhanced for loop.
  • Using collection classes like ArrayList in which modifications during iteration are not supported.

Solutions

  • Use the Iterator interface's remove() method instead of using the collection's remove() method directly during iteration.
  • Consider using concurrent collection classes like CopyOnWriteArrayList or ConcurrentHashMap which are designed to handle modifications in multi-threaded situations.
  • Collect items to be removed in a separate List, and remove them after the iteration completes.

Common Mistakes

Mistake: Using a for-each loop when modifying a collection.

Solution: Switch to an Iterator and use its remove() method for safe element removal.

Mistake: Not synchronizing access to a shared collection in multi-threaded apps.

Solution: Use synchronized blocks or concurrent collection classes to manage access.

Helpers

  • ConcurrentModificationException Java
  • Java Collection modification
  • Java iterator exception
  • fix ConcurrentModificationException in Java
  • Java collection thread safety

Related Questions

⦿How to Query Parent Class Without Loading Subclasses in Hibernate?

Learn how to efficiently query the parent class in a Hibernate tablepersubclass inheritance strategy without loading subclasses.

⦿What is a Server-Side Browser Capable of Executing JavaScript?

Explore how serverside browsers execute JavaScript their use cases and implementation details for web scraping and testing.

⦿How to Create Simple CRUD Applications in Spring Using Existing Database or Hibernate Configurations?

Learn to generate simple CRUD applications in Spring leveraging existing databases or Hibernate settings.

⦿How to Generate an Apache htpasswd Hash Using Java

Learn to generate an Apache htpasswd hash in Java with detailed examples and explanations. Secure your passwords effectively with Java hashing methods.

⦿Understanding Static and Dynamic Class Loading in Programming

Explore the differences between static and dynamic class loading in programming with examples common issues and solutions.

⦿How Can I Parse XML Using SAX or DOM While Retaining Line Numbers for Each Node?

Learn how to parse XML with SAX or DOM in Python while accessing line numbers for each node. Detailed explanation and code examples included.

⦿What Was Sun's Rationale Behind the Implementation of String.hashCode()?

Discover the reasons behind Suns design choices for the String.hashCode method in Java. Learn about its significance and implementation details.

⦿What is the Best Java Web Service Framework for Development?

Explore the top Java web service frameworks for performance ease of use and community support to choose the best one for your project.

⦿Why Does My Java Program Terminate Unexpectedly Without an Error Message?

Discover reasons and solutions for unexpected Java program termination without error messages and improve your debugging skills.

⦿How to Merge Multiple TIFF Images into a Single Multi-page TIFF in Java

Learn how to combine multiple TIFF images into one multipage TIFF using Java with stepbystep instructions and code examples.

© Copyright 2025 - CodingTechRoom.com