hashmap遍历时用map.remove方法为何会报错?

发现问题

笔者最近在调试项目bug的时候,遇到了一个很奇怪的bug,就是在对hashmap集合进行遍历的时候,同时作了remove操做,这个操做最后致使抛出了java.util.ConcurrentModificationException的错误。
带着疑惑,下面参考着源码,分析问题的缘由。
首先,重现问题,构造一个map并往里面加元素:java

private static HashMap<Integer, String> map = new HashMap<Integer, String>();;
	public static void main(String[] args) {
  	        for(int i = 0; i < 10; i++){  
	            map.put(i, "value" + i);  
	        }  
	}
复制代码

而后移除一些元素,此时就会报java.util.ConcurrentModificationException错误bash

for(Map.Entry<Integer, String> entry : map.entrySet()){  
         Integer key = entry.getKey();  
         if(key % 2 == 0){  
             System.out.println("To delete key " + key);  
             map.remove(key);  
             System.out.println("The key " + + key + " was deleted");  
         }  
复制代码

报错

分析问题

从报错中能够看出,HashMap$HashIterator.nextNode这个方法有代码错误了,点进去看,大概知道HashMap.this.modCount != this.expectedModCount 成立并发

再看一下hashmap的remove操做是作了什么:高并发

这里对modCount进行了自增操做,表示操做动做+1。再看modCount和expectedModCount是什么东西this

问题缘由

能够看出迭代器初始化的时候就对modCount和expectedModCount进行同步。
到此,能够看出报错的缘由:spa

  • hashmap里维护了一个modCount变量,迭代器里维护了一个expectedModCount变量,一开始二者是同样的。
  • 每次进行hashmap.remove操做的时候就会对modCount+1,此时迭代器里的expectedModCount仍是以前的值。
  • 在下一次对迭代器进行next()调用时,判断是否HashMap.this.modCount != this.expectedModCount,若是是则抛出异常。

解决问题

那什么状况下在遍历的时候能够删除map里面的元素呢?看下迭代器提供的remove方法:线程

能够看出迭代器里remove了一个元素以后会对expectedModCount从新赋值,这样再次遍历的时候就不会报错了。因此以前的代码能够改为以下写法,直接调用迭代器的remove方法。调试

Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
      while(it.hasNext()){
          Map.Entry<Integer, String> entry = it.next();
          Integer key = entry.getKey();
          if(key % 2 == 0){
         	 System.out.println("To delete key " + key);
         	 it.remove();    
         	 System.out.println("The key " + + key + " was deleted");

          }
      }
复制代码

总结

  • 基本上java集合类(包括list和map)在遍历时没用迭代器进行删除了都会报ConcurrentModificationException错误,这是一种fast-fail的机制,初衷是为了检测bug。
  • 通俗一点来讲,这种机制就是为了防止高并发的状况下,多个线程同时修改map或者list的元素致使的数据不一致,这是只要判断当前modCount != expectedModCount便可以知道有其余线程修改了集合。

替换机制:code

  • 用迭代器的remove方法。
  • 用currentHashMap替代HashMap
相关文章
相关标签/搜索