Iterator的remove方法如何实际删除对象 [英] How Iterator's remove method actually remove an object

查看:822
本文介绍了Iterator的remove方法如何实际删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道,在迭代时从集合中移除对象的最安全的也许只是安全的方法是首先检索 Iterator ,执行循环并在需要时删除;

 迭代器iter = Collection.iterator 
while(iter.hasNext()){
Object o = iter.next()
if(o.equals(what I'm looking for)){
iter。去掉();
}
}

我想了解, t发现了一个深入的技术说明,是如何执行此移除。

如果:

  (Object o:myCollection()。getObjects()){
if(o.equals(what I'm looking for)){
myCollection.remove(o);
}
}

会抛出 ConcurrentModificationException ,在技术术语中 Iterator.remove()是什么?是否删除对象,中断循环并重新启动循环?



我在官方文档中看到:


删除当前元素如果尝试
调用,则抛出 IllegalStateException


部分删除当前元素,让我想起在正常循环=>(执行相等测试和删除,如果需要)发生的完全相同的情况,但为什么Iterator循环ConcurrentModification安全?

解决方案

Iterator如何删除元素取决于它的实现,对于不同的集合可能不同。绝对不会破坏你所在的循环。我只是看看ArrayList迭代器是如何实现的,下面是代码:

  public void remove(){
if(lastRet< 0)
throw new IllegalStateException();
checkForComodification();

try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch(IndexOutOfBoundsException ex){
throw new ConcurrentModificationException();
}
}

所以它检查并发修改, ArrayList remove 方法,并增加列表修改的计数,以便在下次迭代时不会抛出ConcurrentModificationException。


We all know that the safest "and probably only safe" way of removing an object from a collection while iterating it, is by first retrieving the Iterator, perform a loop and remove when needed;

Iterator iter=Collection.iterator();
while(iter.hasNext()){
    Object o=iter.next()
    if(o.equals(what i'm looking for)){
        iter.remove();
    }
}

What I would like to understand, and unfortunately haven't found a deep technical explanation about, is how this removal is performed,
If:

for(Object o:myCollection().getObjects()){
    if(o.equals(what i'm looking for)){
        myCollection.remove(o);
    }
}

Will throw a ConcurrentModificationException, what does "in technical terms" Iterator.remove() do? Does it removes the object, breaks the loop and restart the loop?

I see in the official documentation:

"Removes the current element. Throws IllegalStateException if an attempt is made to call remove() that is not preceded by a call to next( )."

The part "removes the current element", makes me think of the exact same situation happening in a "regular" loop => (perform equality test and remove if needed), but why is the Iterator loop ConcurrentModification-safe?

解决方案

How exactly Iterator removes elements depends on its implementation, which may be different for different Collections. Definitely it doesn't break the loop you're in. I've just looked how ArrayList iterator is implemented and here's the code:

public void remove() {
    if (lastRet < 0)
        throw new IllegalStateException();
    checkForComodification();

    try {
        ArrayList.this.remove(lastRet);
        cursor = lastRet;
        lastRet = -1;
        expectedModCount = modCount;
    } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
    }
}

So it checks for concurrent modifications, removes element using public ArrayList remove method, and increments counter of list modifications so ConcurrentModificationException won't be thrown at next iteration.

这篇关于Iterator的remove方法如何实际删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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