HashMap迭代时不抛出ConcurrentModificationException的特例

简介: <p>         众所周知,HashMap在迭代时remove会抛出异常,ConcurrentModificationException,但事实真的是这样的吗?的确会抛异常,但也有特例。废话少说,上代码:</p> <p></p> <pre code_snippet_id="1603859" snippet_file_name="blog_20160309_1_213269" na

         众所周知,HashMap在迭代时remove会抛出异常,ConcurrentModificationException,但事实真的是这样的吗?的确会抛异常,但也有特例。废话少说,上代码:

public class ConcurrentModificationException {
    public static void main(String[] args) {
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(1, 2);
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            map.remove(1);
        }
        System.out.println(map);
    }
}
运行以上代码,我们将会发现,程序并没有报ConcurrentModificationException异常,而且输出:{}。

        这与常识不服啊?想要弄懂其中缘由,得理解程序何时会抛ConcurrentModificationException异常。通过HashMap的源码可知,当modCount != expectedModCount时,此异常将会被抛出。

  1. if (modCount != expectedModCount)  
  2.                 throw new ConcurrentModificationException(); 
而上面的代码在执行remove()后,接着遍历map.entrySet(),执行到

public final boolean hasNext() {
            return next != null;
        }

但会false,并没有检测modCount != expectedModCount,也就不会抛异常了。

但是,如果我们在上面的代码中再加一行添加键值对的语句:map.put(3, 4);也就是说map中有两个键值对,这时你会发现程序将抛出ConcurrentModificationException异常,这又是为什么呢?还是来看源码就知道了。

执行remove之后,

接着遍历map.entrySet(),执行到

public final boolean hasNext() {
            return next != null;
        }

返回true,接着执行

final class EntryIterator extends HashIterator
        implements Iterator<Map.Entry<K,V>> {
        public final Map.Entry<K,V> next() { return nextNode(); }
    }

跳转到nextNode()中,执行

 final Node<K,V> nextNode() {
            Node<K,V>[] t;
            Node<K,V> e = next;
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            if (e == null)
                throw new NoSuchElementException();
            if ((next = (current = e).next) == null && (t = table) != null) {
                do {} while (index < t.length && (next = t[index++]) == null);
            }
            return e;
        }

看到了吧,上面的

if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
异常就在这里抛出了。

如果进一步深究,你会发现一个更有趣的事,如果你迭代的代码是这样的,Java8的Lambda表达式

map.forEach((key, value) -> {
            System.out.println(key + "==" + value);
            map.remove(1); // ConcurrentModificationException
        });
你会发现,即使你的map中只有一个键值对,程序也将抛出异常,这与foeEach和lambda迭代的内部原理有关了,同样,调试源码将找到答案。
在执行remove后,程序将进入

@Override
    public void forEach(BiConsumer<? super K, ? super V> action) {
        Node<K,V>[] tab;
        if (action == null)
            throw new NullPointerException();
        if (size > 0 && (tab = table) != null) {
            int mc = modCount;
            for (int i = 0; i < tab.length; ++i) {
                for (Node<K,V> e = tab[i]; e != null; e = e.next)
                    action.accept(e.key, e.value);
            }
            if (modCount != mc)
                throw new ConcurrentModificationException();
        }
    }

在这里if (modCount != mc)将抛出异常。


总结:

          HashMap迭代时Remove掉map中包含的键值对,从而改变结构(modCount加1),接下来程序若再有检测modCount的fast-fail机制,程序便将抛出ConcurrentModificationException异常。(注:如果上面程序,remove(6) 将不会引发HashMap结构性改变,也就不会抛异常了)

如果你还了解HashMap的更多源码分析,请移步HashMap源码分析(jdk1.8)




目录
相关文章
|
8月前
|
Java
探索Java集合的3种遍历方式
传统的集合遍历方式 在Java中,我们可以使用传统的循环和迭代器来遍历集合
145 2
|
2月前
Map遍历时报ConcurrentModificationException解决方法
Map遍历时报ConcurrentModificationException解决方法
|
9月前
|
Java
ArrayList 循环遍历并删除元素的常见陷阱
ArrayList 循环遍历并删除元素的常见陷阱
62 0
|
10月前
for-each或迭代器中调用List的remove方法会抛出ConcurrentModificationException的原因
for-each循环遍历的实质是迭代器,使用迭代器的remove方法前必须调用一下next()方法,并且调用一次next()方法后是不允许多次调用remove方法的,为什么呢?接下来一起来看吧
55 0
你真的明白关于迭代器的方法、使用异常、并发修改异常介绍嘛?
关于迭代器的方法、使用异常、并发修改异常介绍的使用
100 0
你真的明白关于迭代器的方法、使用异常、并发修改异常介绍嘛?
|
存储 JSON fastjson
ArrayList的toArray()方法为啥不利用泛型返回List的泛型类型的数组探究
ArrayList的toArray()方法为啥不利用泛型返回List的泛型类型的数组探究
127 0
|
Java
List抛出ConcurrentModificationException
版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82796593 ...
1304 0
|
Java 存储
Java for循环删除ArrayList重复元素陷阱,Iterator迭代器遍历删除重复元素
Java for循环删除ArrayList重复元素陷阱,Iterator迭代器遍历删除重复元素 这是一个一不留神就犯下错误的Java列表ArrayList重复删除元素陷阱,并且问题比较隐蔽。
2066 0