java应用之ConcurrentModificationException

举报
yd_273762914 发表于 2020/12/03 00:48:05 2020/12/03
【摘要】 我们要写个遍历Map集合,删除指定key值的方法,我们估计会这样写。 刚开始我习惯上会写上map.remove(entry.getKey()),remove集合的一个值。但是写了之后,会发现java.util.ConcurrentModificationException异常。 private static void deleteKeyOfMap(String mke...

我们要写个遍历Map集合,删除指定key值的方法,我们估计会这样写。
刚开始我习惯上会写上map.remove(entry.getKey()),remove集合的一个值。但是写了之后,会发现java.util.ConcurrentModificationException异常。

private static void deleteKeyOfMap(String mkey,Map<String,Object> paramsMap){ Iterator<Map.Entry<String,Object>> iter = map.entrySet().iterator();
		while(iter.hasNext()){ Map.Entry<String, Object> entry = iter.next(); if(entry.getKey().equals(k)){ map.remove(entry.getKey()); //iter.remove(); }
		}
	}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

这是什么异常呢?
总结起来其实是集合的安全性决定的,可以看看HashMap的源码

private abstract class HashIterator<E> implements Iterator<E> { Entry<K,V> next; // next entry to return int expectedModCount;   // For fast-fail int index; // current slot Entry<K,V> current; // current entry HashIterator() { expectedModCount = modCount; if (size > 0) { // advance to first entry Entry[] t = table; while (index < t.length && (next = t[index++]) == null) ; } } public final boolean hasNext() { return next != null; } final Entry<K,V> nextEntry() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); Entry<K,V> e = next; if (e == null) throw new NoSuchElementException(); if ((next = e.next) == null) { Entry[] t = table; while (index < t.length && (next = t[index++]) == null) ; } current = e; return e; } public void remove() { if (current == null) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); Object k = current.key; current = null; HashMap.this.removeEntryForKey(k); expectedModCount = modCount; } }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

找到源码关键的一句expectedModCount = modCount;
初始化时,modCount赋值给expectedModCount。

跟一下代码:

 public void remove() { if (current == null) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); Object k = current.key; current = null; HashMap.this.removeEntryForKey(k);//在跟一下removeEntryForKey方法 expectedModCount = modCount; }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
/** * Removes and returns the entry associated with the specified key * in the HashMap.  Returns null if the HashMap contains no mapping * for this key. */ final Entry<K,V> removeEntryForKey(Object key) { if (size == 0) { return null; } int hash = (key == null) ? 0 : hash(key); int i = indexFor(hash, table.length); Entry<K,V> prev = table[i]; Entry<K,V> e = prev; while (e != null) { Entry<K,V> next = e.next; Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) { modCount++;//modCount+1 size--; if (prev == e) table[i] = next; else prev.next = next; e.recordRemoval(this); return e; } prev = e; e = next; } return e; }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

从源码可以看出,调用HashMap的remove方法modCount加1了,所以会导致
expectedModCount != modCount;

这就是java.util.ConcurrentModificationException出现的原因

集合本身这样设计是为了安全性考虑,在Iterator遍历时,不允许被调用remove等等方法

所以正确的代码应该这样改

private static void deleteKeyOfMap(String mkey,Map<String,Object> paramsMap){ Iterator<Map.Entry<String,Object>> iter = map.entrySet().iterator();
		while(iter.hasNext()){ Map.Entry<String, Object> entry = iter.next(); if(entry.getKey().equals(k)){ //map.remove(entry.getKey()); iter.remove(); }
		}
	}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

附录参考资料:

Java迭代foreach原理解析

文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。

原文链接:smilenicky.blog.csdn.net/article/details/79786087

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。