日韩性视频-久久久蜜桃-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--转的全部內容,希望文章能夠幫你解決所遇到的問題。

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