How to Remove an Object from an ArrayList Using a For-Each Loop in Java?

Question

How can I remove an object from an ArrayList while iterating over it using a for-each loop in Java?

ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");

for(String item : list) {
    if(item.equals("B")) {
        list.remove(item); // This will cause ConcurrentModificationException
    }
}

Answer

Removing elements from a Java ArrayList while iterating over it using a for-each loop is problematic because it can lead to a ConcurrentModificationException. Instead, consider using an Iterator or filter the list based on certain conditions before deletion.

Iterator<String> iterator = list.iterator();

while(iterator.hasNext()) {
    String item = iterator.next();
    if(item.equals("B")) {
        iterator.remove(); // Safely removes the current element
    }
}

Causes

  • Using for-each loop which modifies the ArrayList directly leads to ConcurrentModificationException.
  • The iterator does not recognize the changes made to the collection during traversal.

Solutions

  • Use an explicit Iterator to safely remove elements during iteration.
  • Create a separate list to collect elements to be removed and delete them after the iteration.
  • Utilize Java 8 Streams for filtering elements without modifying the original list.

Common Mistakes

Mistake: Attempting to remove an object from the ArrayList with a for-each loop directly.

Solution: Switch to using an explicit Iterator or store them for removal after the loop.

Mistake: Confusion between removing elements during iteration which causes exceptions.

Solution: Understand the flow of the for-each loop and the underlying collection modification rules.

Helpers

  • Java ArrayList remove object
  • for-each loop remove element
  • ConcurrentModificationException
  • Java Iterator remove
  • Java Collections best practices

Related Questions

⦿How to Scroll to a Selected Item in a Java JList?

Learn how to programmatically scroll to a selected item in a Java JList for improved user navigation. Follow this detailed guide for stepbystep instructions.

⦿How to Resolve 'Got Different Size of Tuples and Aliases' Exception After Migrating to Spring Boot 2.0.0.RELEASE?

Learn how to fix the Got different size of tuples and aliases exception after upgrading to Spring Boot 2.0.0.RELEASE with our detailed guide.

⦿How to Send Notifications to an Android App from a Java Server Using Google Cloud Messaging (GCM)

Learn how to send push notifications from your Java server to an Android application using Google Cloud Messaging GCM with this detailed guide.

⦿When Will the Java Date Object Collide or Collapse Due to Uniqueness Limitations?

Explore the potential limitations of Javas Date object including issues of date collisions and how to handle them in your applications.

⦿How to Resolve ObjectInputStream Blocking Issues in Java?

Learn the common causes and solutions for ObjectInputStream blocking problems in Java. Expert tips and code examples included.

⦿How to Skip Loops While Debugging Java Code?

Learn how to effectively skip loops while debugging Java code to streamline your debugging process.

⦿How to Determine if a String Contains Lowercase, Uppercase, Special Characters, and Digits?

Learn how to check if a string contains lowercase uppercase letters special characters and digits using Python. Detailed examples included.

⦿How Can I Generalize an Apache ANT Target?

Learn how to generalize an Apache ANT target for improved build flexibility with expert tips and examples.

⦿How to Change the Text Color of a Button in DatePickerDialog?

Learn how to customize the button text color in DatePickerDialog with stepbystep instructions and code snippets.

⦿Why Does a Generated Certificate Stop Working When Moved to the Resources Folder?

Explore why moving a generated certificate to the resources folder can cause issues along with solutions and common mistakes.

© Copyright 2025 - CodingTechRoom.com