简体   繁体   English

如果HashSet,ArrayList返回的迭代器是快速失败的,我们如何使用迭代器remove()从Collection中删除元素

[英]If Iterators returned by HashSet, ArrayList are fail-fast, how can we use iterator remove() to remove elements from Collection

As we all know that Iterator s returned by ArrayList , HashMap , HashSet etc. are fail-fast, but while using iterator's remove() doesn't throw ConcurrentModificationException . 众所周知, ArrayListHashMapHashSet等返回的Iterator是快速失败的,但是在使用迭代器的remove()不会抛出ConcurrentModificationException How? 怎么样?

fail-fast iterators throw ConcurrentModificationException if a collection is modified while iterating over it. 如果在迭代集合时对其进行了修改,则快速失败迭代器将引发ConcurrentModificationException But while removing elements from ArrayList or HashSet using iterator's remove() , doesn't throw any ConcurrentModificationException . 但是,在使用迭代器的remove()ArrayListHashSet删除元素时,不会引发任何ConcurrentModificationException Please explain elaborately. 请详细说明。

Thanks 谢谢

but while using iterator's remove() doesn't throw ConcurrentModificationException. 但是在使用迭代器的remove()时不会引发ConcurrentModificationException。 How? 怎么样?

Because that behaviour is explicitly documented. 因为该行为已明确记录。

For ArrayList : 对于ArrayList

if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException . 如果在创建迭代器之后的任何时间对列表进行结构修改,除非通过迭代器自己的removeadd方法,否则迭代器将抛出ConcurrentModificationException

For HashSet : 对于HashSet

if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the Iterator throws a ConcurrentModificationException . 如果在创建迭代器之后的任何时候修改了集合,则除了通过迭代器自己的remove方法以外,都以其他方式修改了该迭代器,则Iterator抛出ConcurrentModificationException

If instead you're asking how that's achieved , well, the source code (at least for OpenJDK) is freely available (and likely explorable directly from your IDE) :) 相反,如果您询问如何实现 ,那么,源代码(至少对于OpenJDK是免费的)(并且很可能直接从您的IDE中探索):)

It is achieved as follows: 它的实现如下:

The collection contains a private int member called modificationCount (or something like that.) Each time you use one of the methods that modify the collection, this modificationCount is incremented. 集合包含一个private int成员,称为modificationCount (或类似的东西)。每次您使用修改集合的方法之一时,此modificationCount都会增加。 When you create an iterator on a collection, the iterator takes notice of the current value of modificationCount of the collection, and each time you invoke the iterator it makes sure that modificationCount has not changed, so as to ensure that the collection has not been modified while iterating. 在集合上创建迭代器时,迭代器会注意到该集合的modificationCount的当前值,并且每次调用该迭代器时,请确保modificationCount未被更改,以确保未修改集合。同时进行迭代。 If the iterator finds that modificationCount has changed, it throws a ConcurrentModificationException . 如果迭代器发现modificationCount已更改,则抛出ConcurrentModificationException

When you delete via the iterator , the iterator refrains from incrementing the modificationCount of the collection. 当您通过iterator删除时, iterator将避免增加集合的modificationCount It is that simple. 就这么简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM