[英]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
. 众所周知,
ArrayList
, HashMap
, HashSet
等返回的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()
从ArrayList
或HashSet
删除元素时,不会引发任何ConcurrentModificationException
。 Please explain elaborately. 请详细说明。
Thanks 谢谢
but while using iterator's remove() doesn't throw ConcurrentModificationException.
但是在使用迭代器的remove()时不会引发ConcurrentModificationException。 How?
怎么样?
Because that behaviour is explicitly documented. 因为该行为已明确记录。
if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own
remove
oradd
methods, the iterator will throw aConcurrentModificationException
.如果在创建迭代器之后的任何时间对列表进行结构修改,除非通过迭代器自己的
remove
或add
方法,否则迭代器将抛出ConcurrentModificationException
。
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 aConcurrentModificationException
.如果在创建迭代器之后的任何时候修改了集合,则除了通过迭代器自己的
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.