我们都知道,由于ConcurrentModificationException异常,您不能执行以下操作:

for (Object i : l) {
    if (condition(i)) {
        l.remove(i);
    }
}

但这显然有时有效,但并非总是如此。下面是一些特定的代码:

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

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

    for (int i : l) {
        if (i == 5) {
            l.remove(i);
        }
    }

    System.out.println(l);
}

当然,这会导致:

Exception in thread "main" java.util.ConcurrentModificationException

即使多线程没有这样做。无论如何。

这个问题的最佳解决方案是什么?如何在循环中从集合中删除项而不抛出此异常?

这里我也用了一个任意的集合,不一定是数组列表,所以你不能依赖get。


当前回答

ConcurrentHashMap或ConcurrentLinkedQueue或ConcurrentSkipListMap可能是另一个选项,因为它们永远不会抛出任何ConcurrentModificationException,即使您删除或添加项目。

其他回答

在Java 8中,你可以使用新的removeIf方法。应用于你的例子:

Collection<Integer> coll = new ArrayList<>();
//populate

coll.removeIf(i -> i == 5);

现在,你可以用下面的代码删除

l.removeIf(current -> current == 5);

ListIterator允许您在列表中添加或删除项目。假设你有一个Car对象列表:

List<Car> cars = ArrayList<>();
// add cars here...

for (ListIterator<Car> carIterator = cars.listIterator();  carIterator.hasNext(); )
{
   if (<some-condition>)
   { 
      carIterator().remove()
   }
   else if (<some-other-condition>)
   { 
      carIterator().add(aNewCar);
   }
}

Java并发修改异常

单线程

Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
    String value = iter.next()
    if (value == "A") {
        list.remove(it.next()); //throws ConcurrentModificationException
    }
}

解决方案:迭代器remove()方法

Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
    String value = iter.next()
    if (value == "A") {
        it.remove()
    }
}

多线

复制/转换并遍历另一个集合。小型收藏 同步(约) 线程安全收集[关于]

I know this question assumes just a Collection, and not more specifically any List. But for those reading this question who are indeed working with a List reference, you can avoid ConcurrentModificationException with a while-loop (while modifying within it) instead if you want to avoid Iterator (either if you want to avoid it in general, or avoid it specifically to achieve a looping order different from start-to-end stopping at each element [which I believe is the only order Iterator itself can do]):

*更新:参见下面的注释,说明类似的情况也可以用传统的for循环实现。

final List<Integer> list = new ArrayList<>();
for(int i = 0; i < 10; ++i){
    list.add(i);
}

int i = 1;
while(i < list.size()){
    if(list.get(i) % 2 == 0){
        list.remove(i++);

    } else {
        i += 2;
    }
}

该代码中没有ConcurrentModificationException。

在这里,我们看到循环没有从开始处开始,也没有在每个元素处停止(我相信Iterator本身不能做到这一点)。

在FWIW中,我们还看到get在list上被调用,如果它的引用只是Collection(而不是更具体的list类型的Collection),则无法做到这一点——list接口包括get,但Collection接口不包括。如果不是因为这个区别,那么列表引用可以是一个集合[因此从技术上讲,这个答案将是一个直接答案,而不是一个切向答案]。

FWIWW相同的代码在修改为start at beginning at stop at每个元素后仍然有效(就像Iterator order一样):

final List<Integer> list = new ArrayList<>();
for(int i = 0; i < 10; ++i){
    list.add(i);
}

int i = 0;
while(i < list.size()){
    if(list.get(i) % 2 == 0){
        list.remove(i);

    } else {
        ++i;
    }
}