AroundAround
Sign in Sign up
Menu
  • Home
    • Home
  • Blog
    Technology
    All Technology Software Engineering Web Applications Java Spring Framework Spring Boot AWS
    All Posts
    Smash Posts Transportation Posts Driving Posts Multi Location Post
    Transport
    Driving Articles Driving License Resources Refresh Drive Success Stories Special Articles
    Research
    Popular lists Weblogs Tutorials
    News
    Education Health Sports
    Traffic Signs
    Dubai Traffic Signs Qatar Traffic Signs Abu Dhabi Traffic Signs Saudi Arabia Traffic Signs Kuwait Traffic Signs Kerala Traffic Signs
  • Insights
    Interview Series
    All Interview Series Java Questions Spring Questions Microservices Questions Database Questions
    Lifestyle
    Umrah Articles Dubai UAE visa 180 days calculator Qatar residence visa 180 days calculator Prayer Time Malappuram Sqm to Cent calculator Kerala
    City Explorer
    Sultan Bathery
  • Mock Tests
    Driving
    Test Home
    Dubai
    RTA Theory Test Parking Knowledge Test Deep-dive Assessment Test
    Abu Dhabi
    RTA Driving Theory Test Deep-dive Assessment Test Saudi Arabia Computer Test
    Other Regions
    Qatar Driving Theory Test Kuwait Driving Theory Test Ajman RTA Theory Test Sharjah RTA Theory Test Bahrain Driving Theory Test Learners License Test - Kerala
    Education
    Kindergarten School
  • Course & Videos
    • CoursesEnroll today
    • Videos20+Watch & Subscribe
  • Questions and Tags
    Topics
    Don't miss Million dollar questions Million Views Day to day life Interview Junky Trendy questions Theory wizard
    Technology
    Java Spring AWS
    Find the differences
    All Find the differences List
    Tags
    macOS AWS Spring
  • About Us
    • Privacy Policy
    • Contact
    • Terms & Conditions
    • Cancellation & Refund Policy
    • Shipping & Delivery Policy
  1. Home
  2. java
  3. Post

Avoiding ConcurrentModificationException when removing from a collection in a loop

Emma Brown
admin
#java #programming
Share post:
Share

Consistently failing `ConcurrentModificationException`? Even without threads? Solve this Java iteration puzzle and avoid the dreaded exception. Click to see how!

Why does iterating and removing from a `Collection` (not just `ArrayList`) cause a `ConcurrentModificationException`, even without multithreading? How can I safely remove items during iteration?

Solution in a Nutshell

Java's ConcurrentModificationException arises when iterating and modifying a collection concurrently. Avoid this using an Iterator's remove() method or copying to a new list.

Method 1: Using Iterator

List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
    String item = iterator.next();
    if (item.equals("b")) {
        iterator.remove();
    }
}

Method 2: Copying to a new list (for simpler logic)

List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> newList = new ArrayList<>();
for (String item : list) {
    if (!item.equals("b")) {
        newList.add(item);
    }
}
list = newList; //Replace old list

Method 3: Using streams (Java 8+) for conciseness:

List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
list.removeIf(item -> item.equals("b"));

Spring Boot Context: These methods apply equally to Spring Boot applications. The exception isn't specific to Spring. Choose the method best suited to your code's readability and performance needs. Using an Iterator is generally preferred for efficiency when removing many items. Streams offer conciseness but might be less performant for massive lists. Copying is simplest for small lists and straightforward logic. Remember to handle potential NullPointerExceptions if your list might contain null values.

Sidebar

Search

Related

Post

`instanceof` null check needed?

Post

Isqrt Integer Check

Post

Java Array Concatenation

Post

File I/O: Create and Write

Tags

#java #programming

Trending posts

Post

Getters and Setters: Why Bother?

Post

Random String Outputting "Hello World"?

Post

Sorting a Map by Value

Post

Java: String to Enum

  • Contact Us
  • Privacy Policy

© Copyright - smashplus 2013-25.