加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java ConcurrentHashMap Example and Iterator--转

发布时间:2020-12-14 06:21:54 所属栏目:Java 来源:网络整理
导读:原文地址: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? ConcurrentModificationEx

原文地址: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??implementation.

Java ConcurrentHashMap

Java 1.5 has introduced?java.util.concurrent?package with??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

<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.HashMap;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.Iterator;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.Map;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.concurrent.ConcurrentHashMap;

<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> ConcurrentHashMapExample {

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; main(String[] args) {

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;ConcurrentHashMap</span>
    Map<String,String> myMap = <span style="color: #0000ff;"&gt;new</span> ConcurrentHashMap<String,String><span style="color: #000000;"&gt;();
    myMap.put(</span>"1","1"<span style="color: #000000;"&gt;);
    myMap.put(</span>"2","1"<span style="color: #000000;"&gt;);
    myMap.put(</span>"3","1"<span style="color: #000000;"&gt;);
    myMap.put(</span>"4","1"<span style="color: #000000;"&gt;);
    myMap.put(</span>"5","1"<span style="color: #000000;"&gt;);
    myMap.put(</span>"6","1"<span style="color: #000000;"&gt;);
    System.out.println(</span>"ConcurrentHashMap before iterator: "+<span style="color: #000000;"&gt;myMap);
    Iterator</span><String> it =<span style="color: #000000;"&gt; myMap.keySet().iterator();

    </span><span style="color: #0000ff;"&gt;while</span><span style="color: #000000;"&gt;(it.hasNext()){
        String key </span>=<span style="color: #000000;"&gt; it.next();
        </span><span style="color: #0000ff;"&gt;if</span>(key.equals("3")) myMap.put(key+"new","new3"<span style="color: #000000;"&gt;);
    }
    System.out.println(</span>"ConcurrentHashMap after iterator: "+<span style="color: #000000;"&gt;myMap);

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;HashMap</span>
    myMap = <span style="color: #0000ff;"&gt;new</span> HashMap<String,"1"<span style="color: #000000;"&gt;);
    System.out.println(</span>"HashMap before iterator: "+<span style="color: #000000;"&gt;myMap);
    Iterator</span><String> it1 =<span style="color: #000000;"&gt; myMap.keySet().iterator();

    </span><span style="color: #0000ff;"&gt;while</span><span style="color: #000000;"&gt;(it1.hasNext()){
        String key </span>=<span style="color: #000000;"&gt; it1.next();
        </span><span style="color: #0000ff;"&gt;if</span>(key.equals("3")) myMap.put(key+"new","new3"<span style="color: #000000;"&gt;);
    }
    System.out.println(</span>"HashMap after iterator: "+<span style="color: #000000;"&gt;myMap);
}

}

When we try to run the above class,output is

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:


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


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.


Now execute the modified code and the output will be:


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.


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?.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读