Java 即使使用iterator.remove()删除节点,HashMap也会抛出ConcurrentModification
操作Java 即使使用iterator.remove()删除节点,HashMap也会抛出ConcurrentModification,java,android,hashmap,Java,Android,Hashmap,操作HashMap时,我遇到了一个ConcurrentModificationException。代码如下: Iterator<Integer> iterator =cacheMap.keySet().iterator(); while(iterator.hasNext()) { if(iterator.next() == target) { iterator.remove(); } } 调用n
HashMap
时,我遇到了一个ConcurrentModificationException
。代码如下:
Iterator<Integer> iterator =cacheMap.keySet().iterator();
while(iterator.hasNext()) {
if(iterator.next() == target) {
iterator.remove();
}
}
调用
nextEntry()
和remove
时,会检查modCount
和expectedModeCount
。但是,如果调用iterator.remove()
来删除节点,则两个整数的差似乎是不可能的。您不能迭代集合并从中删除项
使用:
Set Set=newhashset();
迭代器迭代器=cacheMap.keySet().Iterator();
while(iterator.hasNext()){
整数键=迭代器.next();
如果(键==目标){
set.add(键);
}
}
cacheMap.rmoveAll(集合);
不能迭代集合并从中删除项
使用:
Set Set=newhashset();
迭代器迭代器=cacheMap.keySet().Iterator();
while(iterator.hasNext()){
整数键=迭代器.next();
如果(键==目标){
set.add(键);
}
}
cacheMap.rmoveAll(集合);
只需使用cacheMap.remove(目标)即可
从文档中:
公共V删除(对象密钥)
从此映射中删除指定键的映射(如果存在)
请记住,HashMap只能为给定的键存储一个对象,因此无需迭代所有值。只需使用cacheMap.remove(目标)
从文档中:
公共V删除(对象密钥)
从此映射中删除指定键的映射(如果存在)
请记住,HashMap只能为给定的键存储一个对象,因此无需对所有值进行迭代。您使用迭代器的代码是否可能位于另一个线程上?显示如何使用
cacheMap
的代码也可能会有所帮助。此处显示的代码是地图输入集的代码,而不是键集的代码;尝试在映射上直接使用迭代器,并从中删除()(尽管javadoc说这不会有什么区别)。KeySet
是这样的,private final class keyterator extensed hash迭代器实现迭代器{public K next(){return nextEntry().key;}
所以我认为这无关紧要。那么.remove()
的代码呢?好吧,不管怎样,试试看。如果它有效,您可能已经在Android的HashMap
中发现了一个bug!为什么要在HashMap
上迭代以从中删除?你为什么要重复它呢?这已经是一个设计错误的迹象了。您正在使用迭代器的代码是否可能在另一个线程上?显示如何使用cacheMap
的代码也可能会有所帮助。此处显示的代码是地图输入集的代码,而不是键集的代码;尝试在映射上直接使用迭代器,并从中删除()(尽管javadoc说这不会有什么区别)。KeySet
是这样的,private final class keyterator extensed hash迭代器实现迭代器{public K next(){return nextEntry().key;}
所以我认为这无关紧要。那么.remove()
的代码呢?好吧,不管怎样,试试看。如果它有效,您可能已经在Android的HashMap
中发现了一个bug!为什么要在HashMap
上迭代以从中删除?你为什么要重复它呢?这已经是一个设计错误的迹象,这是错误的。你可以。否则,迭代器将不会定义.remove()
!这是错误的。你可以。否则,迭代器将不会定义.remove()
!
@Override public Set<K> keySet() {
Set<K> ks = keySet;
return (ks != null) ? ks : (keySet = new KeySet());
}
private final class KeyIterator extends HashIterator
implements Iterator<K> {
public K next() { return nextEntry().key; }
}
private abstract class HashIterator {
int nextIndex;
HashMapEntry<K, V> nextEntry = entryForNullKey;
HashMapEntry<K, V> lastEntryReturned;
int expectedModCount = modCount;
HashIterator() {
if (nextEntry == null) {
HashMapEntry<K, V>[] tab = table;
HashMapEntry<K, V> next = null;
while (next == null && nextIndex < tab.length) {
next = tab[nextIndex++];
}
nextEntry = next;
}
}
public boolean hasNext() {
return nextEntry != null;
}
HashMapEntry<K, V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (nextEntry == null)
throw new NoSuchElementException();
HashMapEntry<K, V> entryToReturn = nextEntry;
HashMapEntry<K, V>[] tab = table;
HashMapEntry<K, V> next = entryToReturn.next;
while (next == null && nextIndex < tab.length) {
next = tab[nextIndex++];
}
nextEntry = next;
return lastEntryReturned = entryToReturn;
}
public void remove() {
if (lastEntryReturned == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
HashMap.this.remove(lastEntryReturned.key);
lastEntryReturned = null;
expectedModCount = modCount;
}
}
@Override public V remove(Object key) {
if (key == null) {
return removeNullKey();
}
int hash = secondaryHash(key);
HashMapEntry<K, V>[] tab = table;
int index = hash & (tab.length - 1);
for (HashMapEntry<K, V> e = tab[index], prev = null;
e != null; prev = e, e = e.next) {
if (e.hash == hash && key.equals(e.key)) {
if (prev == null) {
tab[index] = e.next;
} else {
prev.next = e.next;
}
modCount++;
size--;
postRemove(e);
return e.value;
}
}
return null;
}
private V removeNullKey() {
HashMapEntry<K, V> e = entryForNullKey;
if (e == null) {
return null;
}
entryForNullKey = null;
modCount++;
size--;
postRemove(e);
return e.value;
}
/**
* Subclass overrides this method to unlink entry.
*/
void postRemove(HashMapEntry<K, V> e) { }
Set<Integer> set = new HashSet<Integer>();
Iterator<Integer> iterator =cacheMap.keySet().iterator();
while(iterator.hasNext()) {
Integer key = iterator.next();
if(key == target) {
set.add(key );
}
}
cacheMap.rmoveAll(set);