日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java ConcurrentHashMap Example and Iterator--转

發布時間:2025/4/5 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java ConcurrentHashMap Example and Iterator--转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:http://www.journaldev.com/122/java-concurrenthashmap-example-iterator#comment-27448

Today we will look into Java ConcurrentHashMap Example. If you are a Java Developer, I am sure that you must be aware of?ConcurrentModificationException?that comes when you want to modify the Collection object while using iterator to go through with all its element. Actually Java Collection Framework iterator is great example of?iterator design pattern?implementation.

Java ConcurrentHashMap

Java 1.5 has introduced?java.util.concurrent?package with?Collection classes?implementations that allow you to modify your collection objects at runtime.

ConcurrentHashMap Example

ConcurrentHashMap?is the class that is similar to HashMap but works fine when you try to modify your map at runtime.

Lets run a sample program to explore this:

ConcurrentHashMapExample.java

package com.journaldev.util;import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap;public class ConcurrentHashMapExample {public static void main(String[] args) {//ConcurrentHashMapMap<String,String> myMap = new ConcurrentHashMap<String,String>();myMap.put("1", "1");myMap.put("2", "1");myMap.put("3", "1");myMap.put("4", "1");myMap.put("5", "1");myMap.put("6", "1");System.out.println("ConcurrentHashMap before iterator: "+myMap);Iterator<String> it = myMap.keySet().iterator();while(it.hasNext()){String key = it.next();if(key.equals("3")) myMap.put(key+"new", "new3");}System.out.println("ConcurrentHashMap after iterator: "+myMap);//HashMapmyMap = new HashMap<String,String>();myMap.put("1", "1");myMap.put("2", "1");myMap.put("3", "1");myMap.put("4", "1");myMap.put("5", "1");myMap.put("6", "1");System.out.println("HashMap before iterator: "+myMap);Iterator<String> it1 = myMap.keySet().iterator();while(it1.hasNext()){String key = it1.next();if(key.equals("3")) myMap.put(key+"new", "new3");}System.out.println("HashMap after iterator: "+myMap);}}

When we try to run the above class, output is

ConcurrentHashMap before iterator: {1=1, 5=1, 6=1, 3=1, 4=1, 2=1} ConcurrentHashMap after iterator: {1=1, 3new=new3, 5=1, 6=1, 3=1, 4=1, 2=1} HashMap before iterator: {3=1, 2=1, 1=1, 6=1, 5=1, 4=1} Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) at java.util.HashMap$KeyIterator.next(HashMap.java:828) at com.test.ConcurrentHashMapExample.main(ConcurrentHashMapExample.java:44)

Looking at the output, its clear that?ConcurrentHashMap?takes care of any new entry in the map whereas HashMap throws?ConcurrentModificationException.

Lets look at the exception stack trace closely. The statement that has thrown Exception is:

String key = it1.next();

It means that the new entry got inserted in the HashMap but Iterator is failing. Actually Iterator on Collection objects are?fail-fast?i.e any modification in the structure or the number of entry in the collection object will trigger this exception thrown by iterator.

So How does iterator knows that there has been some modification in the HashMap. We have taken the set of keys from HashMap once and then iterating over it.

HashMap contains a variable to count the number of modifications and iterator use it when you call its next() function to get the next entry.

HashMap.java

/*** The number of times this HashMap has been structurally modified* Structural modifications are those that change the number of mappings in* the HashMap or otherwise modify its internal structure (e.g.,* rehash). This field is used to make iterators on Collection-views of* the HashMap fail-fast. (See ConcurrentModificationException).*/transient volatile int modCount;

Now to prove above point, lets change the code a little bit to come out of the iterator loop when we insert the new entry. All we need to do is add a break statement after the put call.

if(key.equals("3")){ myMap.put(key+"new", "new3"); break; }

Now execute the modified code and the output will be:

ConcurrentHashMap before iterator: {1=1, 5=1, 6=1, 3=1, 4=1, 2=1} ConcurrentHashMap after iterator: {1=1, 3new=new3, 5=1, 6=1, 3=1, 4=1, 2=1} HashMap before iterator: {3=1, 2=1, 1=1, 6=1, 5=1, 4=1} HashMap after iterator: {3=1, 2=1, 1=1, 3new=new3, 6=1, 5=1, 4=1}

Finally, what if we won’t add a new entry but update the existing key-value pair. Will it throw exception?

Change the code in the original program and check yourself.

//myMap.put(key+"new", "new3"); myMap.put(key, "new3");

If you get confused (or shocked) with the output, comment below and I will be happy to explain it further.

Did you noticed those angle brackets while creating our collection object and Iterator, it’s called generics in java and it’s very powerful when it comes to type-checking at compile time to remove ClassCastException at runtime, learn more about generics in?Java Generics Example.

?

轉載于:https://www.cnblogs.com/davidwang456/p/6016897.html

總結

以上是生活随笔為你收集整理的Java ConcurrentHashMap Example and Iterator--转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 一区二区三区四区精品 | 亚洲一区二区三区成人 | 色视频在线观看 | www.超碰 | 欧美一级做性受免费大片免费 | 免费 成 人 黄 色 | 国产黄 | 神秘马戏团在线观看免费高清中文 | 成人免费福利 | 日本黄色录相 | 国产精品一区二区三区线羞羞网站 | 色黄网站在线观看 | 亚洲一区二区久久久 | 欧美日韩人妻精品一区在线 | 最新色网站 | 99久久99久久精品国产片果冰 | 成年人免费观看视频网站 | 国产精品视频a | 亚洲综合性 | 色一情一乱一伦 | 操日本老妇 | 日色网站 | 超碰caoporen | 亚洲国产精品久久人人爱 | 国产h片在线观看 | 波多野结衣视频在线看 | 日韩精品国产AV | 操碰av| 黑巨茎大战欧美白妞 | 香蕉人人精品 | 国产精品久久久久野外 | 亚洲高清在线视频 | 一本大道久久a久久综合婷婷 | 毛片在线观看网站 | 免费观看理伦片在线播放视频软件 | 人妻无码久久一区二区三区免费 | 国产成人黄色 | 国产免费成人 | 色婷婷小说 | 亚洲涩涩图 | 特级毛片av | 亚洲欧美激情视频 | 成人自拍在线 | 超碰牛牛 | 久久人人人 | 强行糟蹋人妻hd中文 | 草色噜噜噜av在线观看香蕉 | 男女在楼梯上高潮做啪啪 | 在线视频中文 | 色综合久久88色综合天天免费 | 91日韩在线| 男女视频久久 | 日本大尺度电影免费观看全集中文版 | 国产资源网 | 高h乱l高辣h文短篇h | 丁香啪啪综合成人亚洲 | 三日本三级少妇三级99 | 欧美丰满美乳xxⅹ高潮www | 中文字幕在线观看一区 | 色婷婷视频在线 | 撸大师av| 中文字幕欧美另类精品亚洲 | 香港三日本三级少妇66 | 国产免费看片 | 国产精品无码久久久久久电影 | 最新国产露脸在线观看 | 青娱乐久久 | 欧美成人乱码一区二区三区 | 久久日视频 | 欧美成人猛片aaaaaaa | 欧美综合国产 | 成人一区二区精品 | 青青射| 日b免费视频| 伊人春色在线视频 | 久91| 青青草青娱乐 | 在线亚洲+欧美+日本专区 | 国产成人无码一区二区三区在线 | a级黄色网址 | 国产成人 综合 亚洲 | 欧美一区二区三区黄片 | 天堂网在线最新版www中文网 | 中文字幕人妻一区二区 | 一级特黄a | 久久精品日韩 | 久久久久久久久久久免费 | 啪啪一区二区 | 奇米777第四色 | 免费 成 人 黄 色 | 另类在线视频 | 国内自拍第一页 | 91麻豆精品一区二区三区 | 伊人免费在线观看高清版 | 亚洲欧洲综合av | 女仆乖h调教跪趴1v1 | 91夫妻论坛| 亚洲一二三级 | 91麻豆精品91久久久久同性 |