Skip to content

Collection modification while looping #100

@cushon

Description

@cushon

Original issue created by danesh@google.com on 2013-02-28 at 06:54 PM


If you have a loop over a Collection and are modifying the collection within the loop. Currently it will throw a ConcurrentModificationException which is a run-time exception. Ideally this should be caught at compile-time.

Bad example:
  ArrayList<Item> itemList = //...
  for (Item item : itemList) {
     if (someCondition) {
        itemList.remove(item); // Exception thrown here
     }
  }

If someCondition is a rareCondition, then this bug may not be discovered unless sufficiently unit tested and/or until the code hits the rareCondition (which depending on the rarity of the condition, could take a while).

Good example:
Use an Iterator with an Iterator.remove();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions