Java?map為什么不能遍歷的同時(shí)進(jìn)行增刪操作
前段時(shí)間,同事在代碼中KW掃描的時(shí)候出現(xiàn)這樣一條:
上面出現(xiàn)這樣的原因是在使用foreach對(duì)HashMap進(jìn)行遍歷時(shí),同時(shí)進(jìn)行put賦值操作會(huì)有問題,異常ConcurrentModificationException。
于是幫同簡(jiǎn)單的看了一下,印象中集合類在進(jìn)行遍歷時(shí)同時(shí)進(jìn)行刪除或者添加操作時(shí)需要謹(jǐn)慎,一般使用迭代器進(jìn)行操作。
于是告訴同事,應(yīng)該使用迭代器Iterator來(lái)對(duì)集合元素進(jìn)行操作。同事問我為什么?這一下子把我問蒙了?對(duì)啊,只是記得這樣用不可以,但是好像自己從來(lái)沒有細(xì)究過(guò)為什么?
于是今天決定把這個(gè)HashMap遍歷操作好好地研究一番,防止采坑!
foreach循環(huán)?
java foreach 語(yǔ)法是在jdk1.5時(shí)加入的新特性,主要是當(dāng)作for語(yǔ)法的一個(gè)增強(qiáng),那么它的底層到底是怎么實(shí)現(xiàn)的呢?下面我們來(lái)好好研究一下:
foreach 語(yǔ)法內(nèi)部,對(duì)collection是用iterator迭代器來(lái)實(shí)現(xiàn)的,對(duì)數(shù)組是用下標(biāo)遍歷來(lái)實(shí)現(xiàn)。Java 5 及以上的編譯器隱藏了基于iteration和數(shù)組下標(biāo)遍歷的內(nèi)部實(shí)現(xiàn)。
(注意,這里說(shuō)的是“Java編譯器”或Java語(yǔ)言對(duì)其實(shí)現(xiàn)做了隱藏,而不是某段Java代碼對(duì)其實(shí)現(xiàn)做了隱藏,也就是說(shuō),我們?cè)谌魏我欢蜫DK的Java代碼中都找不到這里被隱藏的實(shí)現(xiàn)。這里的實(shí)現(xiàn),隱藏在了Java 編譯器中,查看一段foreach的Java代碼編譯成的字節(jié)碼,從中揣測(cè)它到底是怎么實(shí)現(xiàn)的了)
我們寫一個(gè)例子來(lái)研究一下:
public class HashMapIteratorDemo { String[] arr = {"aa", "bb", "cc"}; public void test1() { for(String str : arr) { } } }
將上面的例子轉(zhuǎn)為字節(jié)碼反編譯一下(主函數(shù)部分):
也許我們不能很清楚這些指令到底有什么作用,但是我們可以對(duì)比一下下面段代碼產(chǎn)生的字節(jié)碼指令:
public class HashMapIteratorDemo2 { String[] arr = {"aa", "bb", "cc"}; public void test1() { for(int i = 0; i < arr.length; i++) { String str = arr[i]; } } }
看看兩個(gè)字節(jié)碼文件,有木有發(fā)現(xiàn)指令幾乎相同,如果還有疑問我們?cè)倏纯磳?duì)集合的foreach操作:
通過(guò)foreach遍歷集合:
public class HashMapIteratorDemo3 { List<Integer> list = new ArrayList<>(); public void test1() { list.add(1); list.add(2); list.add(3); for(Integer var : list) { } } }
通過(guò)Iterator遍歷集合:
public class HashMapIteratorDemo4 { List<Integer> list = new ArrayList<>(); public void test1() { list.add(1); list.add(2); list.add(3); Iterator<Integer> it = list.iterator(); while(it.hasNext()) { Integer var = it.next(); } } }
將兩個(gè)方法的字節(jié)碼對(duì)比如下:
我們發(fā)現(xiàn)兩個(gè)方法字節(jié)碼指令操作幾乎一模一樣;
這樣我們可以得出以下結(jié)論:
對(duì)集合來(lái)說(shuō),由于集合都實(shí)現(xiàn)了Iterator迭代器,foreach語(yǔ)法最終被編譯器轉(zhuǎn)為了對(duì)Iterator.next()的調(diào)用;
對(duì)于數(shù)組來(lái)說(shuō),就是轉(zhuǎn)化為對(duì)數(shù)組中的每一個(gè)元素的循環(huán)引用。
HashMap遍歷集合并對(duì)集合元素進(jìn)行remove、put、add
1、現(xiàn)象
根據(jù)以上分析,我們知道HashMap底層是實(shí)現(xiàn)了Iterator迭代器的 ,那么理論上我們也是可以使用迭代器進(jìn)行遍歷的,這倒是不假,例如下面:
public class HashMapIteratorDemo5 { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "aa"); map.put(2, "bb"); map.put(3, "cc"); for(Map.Entry<Integer, String> entry : map.entrySet()){ int k=entry.getKey(); String v=entry.getValue(); System.out.println(k+" = "+v); } } }
輸出:
ok,遍歷沒有問題,那么操作集合元素remove、put、add呢?
public class HashMapIteratorDemo5 { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "aa"); map.put(2, "bb"); map.put(3, "cc"); for(Map.Entry<Integer, String> entry : map.entrySet()){ int k=entry.getKey(); if(k == 1) { map.put(1, "AA"); } String v=entry.getValue(); System.out.println(k+" = "+v); } } }
執(zhí)行結(jié)果:
執(zhí)行沒有問題,put操作也成功了。
但是!但是!但是!問題來(lái)了?。?!
我們知道HashMap是一個(gè)線程不安全的集合類,如果使用foreach遍歷時(shí),進(jìn)行add,remove操作會(huì)java.util.ConcurrentModificationException異常。put操作可能會(huì)拋出該異常。(為什么說(shuō)可能,這個(gè)我們后面解釋)
為什么會(huì)拋出這個(gè)異常呢?
我們先去看一下java api文檔對(duì)HasMap操作的解釋吧。
翻譯過(guò)來(lái)大致的意思就是該方法是返回此映射中包含的鍵的集合視圖。集合由映射支持,如果在對(duì)集合進(jìn)行迭代時(shí)修改了映射(通過(guò)迭代器自己的移除操作除外),則迭代的結(jié)果是未定義的。集合支持元素移除,通過(guò)Iterator.remove、set.remove、removeAll、retainal和clear操作從映射中移除相應(yīng)的映射。簡(jiǎn)單說(shuō),就是通過(guò)map.entrySet()這種方式遍歷集合時(shí),不能對(duì)集合本身進(jìn)行remove、add等操作,需要使用迭代器進(jìn)行操作。
對(duì)于put操作,如果這個(gè)操作時(shí)替換操作如上例中將第一個(gè)元素進(jìn)行修改,就沒有拋出異常,但是如果是使用put添加元素的操作,則肯定會(huì)拋出異常了。
我們把上面的例子修改一下:
public class HashMapIteratorDemo5 { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "aa"); map.put(2, "bb"); map.put(3, "cc"); for(Map.Entry<Integer, String> entry : map.entrySet()){ int k=entry.getKey(); if(k == 1) { map.put(4, "AA"); } String v=entry.getValue(); System.out.println(k+" = "+v); } } }
執(zhí)行出現(xiàn)異常:
這就是驗(yàn)證了上面說(shuō)的put操作可能會(huì)拋出java.util.ConcurrentModificationException異常。
但是有疑問了,我們上面說(shuō)過(guò)foreach循環(huán)就是通過(guò)迭代器進(jìn)行的遍歷?。繛槭裁吹竭@里是不可以了呢?
這里其實(shí)很簡(jiǎn)單,原因是我們的遍歷操作底層確實(shí)是通過(guò)迭代器進(jìn)行的,但是我們的remove等操作是通過(guò)直接操作map進(jìn)行的,如上例子:map.put(4, "AA");//這里實(shí)際還是直接對(duì)集合進(jìn)行的操作,而不是通過(guò)迭代器進(jìn)行操作。所以依然會(huì)存在ConcurrentModificationException異常問題。
2、細(xì)究底層原理
我們?cè)偃タ纯碒ashMap的源碼,通過(guò)源代碼,我們發(fā)現(xiàn)集合在使用Iterator進(jìn)行遍歷時(shí)都會(huì)用到這個(gè)方法:
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; }
這里modCount是表示map中的元素被修改了幾次(在移除,新加元素時(shí)此值都會(huì)自增),而expectedModCount是表示期望的修改次數(shù),在迭代器構(gòu)造的時(shí)候這兩個(gè)值是相等,如果在遍歷過(guò)程中這兩個(gè)值出現(xiàn)了不同步就會(huì)拋出ConcurrentModificationException異常。
現(xiàn)在我們來(lái)看看集合remove操作:
(1)HashMap本身的remove實(shí)現(xiàn):
public V remove(Object key) { Node<K,V> e; return (e = removeNode(hash(key), key, null, false, true)) == null ? null : e.value; }
(2)HashMap.KeySet的remove實(shí)現(xiàn)
public final boolean remove(Object key) { return removeNode(hash(key), key, null, false, true) != null; }
(3)HashMap.EntrySet的remove實(shí)現(xiàn)
public final boolean remove(Object o) { if (o instanceof Map.Entry) { Map.Entry<?,?> e = (Map.Entry<?,?>) o; Object key = e.getKey(); Object value = e.getValue(); return removeNode(hash(key), key, value, true, true) != null; } return false; }
(4)HashMap.HashIterator的remove方法實(shí)現(xiàn)
public final void remove() { Node<K,V> p = current; if (p == null) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); current = null; K key = p.key; removeNode(hash(key), key, null, false, false); expectedModCount = modCount; //----------------這里將expectedModCount 與modCount進(jìn)行同步 }
以上四種方式都通過(guò)調(diào)用HashMap.removeNode方法來(lái)實(shí)現(xiàn)刪除key的操作。在removeNode方法內(nèi)只要移除了key, modCount就會(huì)執(zhí)行一次自增操作,此時(shí)modCount就與expectedModCount不一致了;
final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable) { Node<K,V>[] tab; Node<K,V> p; int n, index; if ((tab = table) != null && (n = tab.length) > 0 && ... if (node != null && (!matchValue || (v = node.value) == value || (value != null && value.equals(v)))) { if (node instanceof TreeNode) ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable); else if (node == p) tab[index] = node.next; else p.next = node.next; ++modCount; //------------------------這里對(duì)modCount進(jìn)行了自增,可能會(huì)導(dǎo)致后面與expectedModCount不一致 --size; afterNodeRemoval(node); return node; } } return null; }
上面三種remove實(shí)現(xiàn)中,只有第三種iterator的remove方法在調(diào)用完removeNode方法后同步了expectedModCount值與modCount相同,所以在遍歷下個(gè)元素調(diào)用nextNode方法時(shí),iterator方式不會(huì)拋異常。
到這里是不是有一種恍然大明白的感覺呢!
所以,如果需要對(duì)集合遍歷時(shí)進(jìn)行元素操作需要借助Iterator迭代器進(jìn)行,如下:
public class HashMapIteratorDemo5 { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "aa"); map.put(2, "bb"); map.put(3, "cc"); // for(Map.Entry<Integer, String> entry : map.entrySet()){ // int k=entry.getKey(); // // if(k == 1) {// map.put(1, "AA");// }// String v=entry.getValue(); // System.out.println(k+" = "+v); // } Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry<Integer, String> entry = it.next(); int key=entry.getKey(); if(key == 1){ it.remove(); } } } }
到此這篇關(guān)于Java map為什么不能遍歷的同時(shí)進(jìn)行增刪操作的文章就介紹到這了,更多相關(guān)Java map 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
偵聽消息隊(duì)列的Message Listener類示例詳解
Spring AMQP 是基于 Spring 框架的AMQP消息解決方案,提供模板化的發(fā)送和接收消息的抽象層,提供基于消息驅(qū)動(dòng)的 POJO的消息監(jiān)聽等,簡(jiǎn)化了我們對(duì)于RabbitMQ相關(guān)程序的開發(fā),本文給大家介紹偵聽消息隊(duì)列的Message Listener類,感興趣的朋友一起看看吧2023-12-12JNDI在JavaEE中的角色_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了JNDI在JavaEE中的角色,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08SpringBoot中@ConfigurationProperties注解的使用與源碼詳解
這篇文章主要介紹了SpringBoot中@ConfigurationProperties注解的使用與源碼詳解,@ConfigurationProperties注解用于自動(dòng)配置綁定,可以將application.properties配置中的值注入到bean對(duì)象上,需要的朋友可以參考下2023-11-11Kotlin + Retrofit + RxJava簡(jiǎn)單封裝使用詳解
這篇文章主要介紹了Kotlin + Retrofit + RxJava簡(jiǎn)單封裝使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07java并發(fā)編程中實(shí)現(xiàn)可見性的四種可行方案解析
這篇文章主要介紹了java并發(fā)編程中實(shí)現(xiàn)可見性的四種可行方案解析,使用關(guān)鍵字volatile和使用鎖(如synchronized關(guān)鍵字或者java.util.concurrent包中的鎖)來(lái)確保對(duì)共享變量的修改在多線程環(huán)境中能夠正確地被其他線程所觀察到,需要的朋友可以參考下2023-08-08