迭代集合,在循环中修改(删除)时避免ConcurrentModificationException [英] Iterating through a Collection, avoiding ConcurrentModificationException when modifying (removing) in loop

查看:351
本文介绍了迭代集合,在循环中修改(删除)时避免ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道你不能这样做:

We all know you can't do this:

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

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

ConcurrentModificationException 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) {
            l.remove(i);
        }
    }

    System.out.println(l);
}

这当然导致:

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?

我还使用任意集合这里,不一定是 ArrayList ,所以你不能依赖 get

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

推荐答案

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

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

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

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.

来源: docs.oracle>收集界面

同样,如果您有一个 ListIterator 并想要添加项目,那么你可以使用 ListIterator #add ,出于同样的原因,您可以使用 Iterator #remove  —它的目的是允许它。

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.

在你的情况下,你试图从列表中删除,但是如果尝试同样的限制则适用在迭代其内容时,放入 Map

In your case you tried to remove from a list, but the same restriction applies if trying to put into a Map while iterating its content.

这篇关于迭代集合,在循环中修改(删除)时避免ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆