我们都知道,由于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


即使没有多个线程。无论如何。

解决此问题的最佳方法是什么?如何在不引发此异常的情况下循环地从集合中删除项目?

我还在这里使用任意的Collection,不一定是ArrayList,因此您不能依赖get

最佳答案

Iterator.remove()是安全的,可以这样使用:

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 = iterator.next();
    if (string.isEmpty()) {
        // Remove the current element from the iterator and the list.
        iterator.remove();
    }
}


请注意,Iterator.remove()是在迭代期间修改集合的唯一安全方法。如果在进行迭代时以任何其他方式修改了基础集合,则行为未指定。

资料来源: docs.oracle > The Collection Interface



同样,如果您有ListIterator并想要添加项目,则可以使用ListIterator#add,出于相同的原因,您可以使用Iterator#remove-它是为了允许它而设计的。



在您的情况下,您尝试从列表中删除,但是如果在迭代其内容的同时尝试将put转换为Map时,将应用相同的限制。

关于java - 遍历Collection,避免在循环中删除对象时避免ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55779120/

10-12 00:37
查看更多