google / google/error-prone

Collection modification while looping

Open
#100 0 comments 0 reactions 0 assignees View on GitHub
migrated Priority-Medium Type-NewCheck
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

_[Original issue](https://code.google.com/p/error-prone/issues/detail?id=100) 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();

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.