Claudiu Claudiu - 1 year ago 70
Java Question

Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop

We all know you can't do this:

for (Object i : l) {
if (condition(i)) {

etc... this apparently works sometimes, but not always. Here's some specific code:

public static void main(String[] args) {
Collection<Integer> l = new ArrayList<Integer>();

for (int i=0; i < 10; ++i) {
l.add(new Integer(4));
l.add(new Integer(5));
l.add(new Integer(6));

for (Integer i : l) {
if (i.intValue() == 5) {


This, of course, results in:

Exception in thread "main" java.util.ConcurrentModificationException

... even though multiple threads aren't doing it... Anyway.

What's the best solution to this problem? How can I remove an item from the collection in a loop without throwing this exception?

I'm also using an arbitrary
here, not necessarily an
, so you can't rely on

Answer Source

Iterator.remove() is safe, you can use it like this:

List<String> list = new ArrayList<>();

// This is a clever way to create the iterator and call iterator.hasNext() like
// you would do in a while-loop. It would be the same as doing:
//     Iterator<String> iterator = list.iterator();
//     while (iterator.hasNext()) {
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
    String string =;
    if (string.isEmpty()) {
        // Remove the current element from the iterator and the list.

Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.


And similarly, if you have a ListIterator and want to add items, you can use ListIterator#add, for the same reason you can use Iterator#remove — it's designed to allow it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download