How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]

Use an Iterator and call remove():

Iterator<String> iter = myArrayList.iterator();

while (iter.hasNext()) {
    String str = iter.next();

    if (someCondition)
        iter.remove();
}

Leave a Comment