Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

java - 遍历Collection,避免在循环中删除对象时避免ConcurrentModificationException(Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop)

We all know you can't do the following because of ConcurrentModificationException :

(我们都知道,由于ConcurrentModificationException您无法执行以下操作:)

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

But this apparently works sometimes, but not always.

(但这显然有时有效,但并非总是如此。) Here's some specific code:

(这是一些特定的代码:)

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);
}

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 Collection here, not necessarily an ArrayList , so you can't rely on get .

(我还在这里使用任意Collection ,不一定是ArrayList ,所以您不能依赖get 。)

  ask by Claudiu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(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();
    }
}

Note that Iterator.remove() is the only safe way to modify a collection during iteration;

(注意, Iterator.remove()是在迭代过程中修改集合的唯一安全方法。) the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

(如果在进行迭代时以任何其他方式修改了基础集合,则行为未指定。)

Source: docs.oracle > The Collection Interface

(来源: docs.oracle>收集接口)


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.

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


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.

(你的情况,你想从列表中删除,但同样的限制,如果试图putMap ,而迭代其内容。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...