HashMap vs ConcurrentHashMap — 示例及Iterator探秘
来源:互联网 发布:金和网络老十佳 编辑:程序博客网 时间:2024/05/22 14:55
如果你是一名Java开发人员,我能够确定你肯定知道ConcurrentModificationException,它是在使用迭代器遍历集合对象时修改集合对象造成的(并发修改)异常。实际上,Java的集合框架是迭代器设计模式的一个很好的实现。
Java 1.5引入了java.util.concurrent包,其中Collection类的实现允许在运行过程中修改集合对象。
ConcurrentHashMap是一个与HashMap很相似的类,但是它支持在运行时修改集合对象。
让我们通过一个简单的程序来帮助理解:
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) { //ConcurrentHashMap Map<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); //HashMap myMap = 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);}
}
当我们试着运行上面的程序,输出如下:
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
at com.test.ConcurrentHashMapExample.main(ConcurrentHashMapExample.java:44)
查看输出,很明显ConcurrentHashMap可以支持向map中添加新元素,而HashMap则抛出了ConcurrentModificationException。查看异常堆栈记录,可以发现是下面这条语句抛出异常:
String key = it1.next();
这就意味着新的元素在HashMap中已经插入了,但是在迭代器执行时出现错误。事实上,集合对象的迭代器提供快速失败(Fail-Fast)的机制,即修改集合对象结构或者元素数量都会使迭代器触发这个异常。但是迭代器是怎么知道HashMap被修改了呢,我们可以一次取出HashMap的所有Key然后进行遍历。HashMap包含一个修改计数器,当你调用它的next()方法来获取下一个元素时,迭代器将会用到这个计数器。HashMap.java/** * HashMap结构的修改次数 * 结构修改是指:改变了HashMap中mapping的个数或者其中的内部结构(比如,重新计算hash值) * 这个字段在通过Collection操作Hashmap时提供快速失败(Fail-fast)功能。 * (参见 ConcurrentModificationException)。 */transient volatile int modCount;现在为了证明上面的观点,我们对原来的代码做一点修改,使迭代器在插入新的元素后跳出循环。只要在调用put方法后增加一个break:
if(key.equals(“3”)){
myMap.put(key+”new”, “new3”);
break;
}
再执行修改后的代码,会得到下面的输出结果:
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}
最后,如果我们不添加新的元素而是修改已经存在的键值对会不会抛出异常呢?修改原来的程序并且自己验证一下:
//myMap.put(key+”new”, “new3”);
myMap.put(key, “new3”);
“`
如果你对于输出结果感觉困惑或者震惊,在下面评论。我会很乐意给出进一步解释。
你有没有注意到那些我们在创建集合和迭代器时的尖括号,在Java中这叫做泛型,当涉及到编译时的类型检查和去除运行时的ClassCastException的时候会很有帮助。点击这里可以了解更多泛型教程。
原文链接: journaldev 翻译: ImportNew.com - 风恋星
译文链接: http://www.importnew.com/8162.html
[ 转载请保留原文出处、译者和译文链接。]
关于作者: 风恋星
- HashMap vs ConcurrentHashMap — 示例及Iterator探秘
- HashMap vs ConcurrentHashMap — 示例及Iterator探秘
- HashMap vs ConcurrentHashMap — 示例及Iterator探秘
- HashMap vs ConcurrentHashMap vs Collections.synchronizedMap(HashMap)
- HashMap——ConcurrentHashMap
- 聊聊并发——HashMap、HashTable及ConcurrentHashMap
- HashMap——ConcurrentHashMap 姚博文
- hashmap&concurrenthashMap
- ConcurrentHashMap & HashMap
- hashmap iterator
- hashmap,hashTable concurrentHashMap 是否线程安全,及实现
- HashMap, HashSet, HashMap Iterator
- HashMap和ConcurrentHashMap浅析
- HashMap和ConcurrentHashMap浅析
- HashMap和ConcurrentHashMap
- HashMap和ConcurrentHashMap分享
- HashMap 与 ConcurrentHashMap
- HashMap,HashTable,ConcurrentHashMap,ConcurrentSkipListMap
- 获取一个文件夹下的数据排版格式相同的Excel表格的数据
- 学习笔记:zlib+boost 的用法
- Gradle Build Running 太慢了。。。。。。
- Android service、broadcast后台启动Activity
- 微信 WEUI 的 switch button 精简提取
- HashMap vs ConcurrentHashMap — 示例及Iterator探秘
- AndroidStudio Gradle第三依赖统一管理(by 星空武哥)
- Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication
- JDBC
- ajax实现异步文件(图片)上传功能
- tesseract-ocr命令学习
- Intellij Idea 入门(一)--------创建Web项目
- vs2010 C++开发备忘
- 实时网络游戏-断线重连