list中remove的问题,以及 ConcurrentModificationException异常
来源:互联网 发布:php nginx 压力测试 编辑:程序博客网 时间:2024/06/13 10:46
【错误场景1】:set容器,边遍历,边add/remove元素
Set<String> set = new HashSet<String>();
for (int i = 0; i < 10000; i++) {set.add(Integer.toString(i));}for (String str : set) { //或使用iterator来循环,JDK5.0以上,这样的遍历底层也都是iterator实现。
set.add("xxx"); //报错// set.remove(str); //报错
}
【错误场景2】:map容器,边遍历,边remove元素
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 100; i++) {map.put(Integer.toString(i), Integer.toString(i));}for (String str : map.keySet()) {//或使用iterator来循环
map.remove(str); //报错
}
【错误场景3】list容器,边遍历,边add/remove元素
List<String> list = new ArrayList<String>();
for (int i = 0; i < 100; i++) {list.add(Integer.toString(i));}for (Iterator<String> it = list.iterator(); it.hasNext();) {
String val = it.next();if (val.equals("5")) {list.add(val); //报错
// list.remove(val); //报错
}}
【错误原因】
- 对于remove操作,list.remove(o)的时候,只将modCount++,而expectedCount值未变,那么迭代器在取下一个元素的时候,发现该二值不等,则抛ConcurrentModificationException异常。
- 对于add操作,同remove
- 具体可以参看这里:http://hi.baidu.com/sdausea/blog/item/57b2fa3dcb101908bba1672e.html
【解决办法】
- remove:用iterator提供的原生态remove()
add:同remove就错了,iterator没有提供原生的add()方法。真是的,还要用新的容器暂存,然后再遍历结束后,全部添加到原容器当中。
set/list:这两类常用容器,就用上面说的方法remove(), add()就好了。
map:直接使用ConcurrentHashMap就ok。为什么别的容器,不也实现个concurrent版本直接用。。?库里不搞,自己搞。
【正确使用案例】
for (Iterator<String> it = list.iterator(); it.hasNext();) {
String val = it.next();if (val.equals("5")) {it.remove();}}
List<String> newList = new ArrayList<String>();
for (Iterator<String> it = list.iterator(); it.hasNext();) {
String val = it.next();if (val.equals("5")) {newList.add(val);}}list.addAll(newList);产生ConcurrentModificationException的原因就是: 执行remove(Object o)方法之后,modCount和expectedModCount不相等了。然后当代码执行到next()方法时,判断了checkForComodification(),发现两个数值不等,就抛出了该Exception。 要避免这个Exception,就应该使用remove()方法。 这里我们就不看add(Object o)方法了,也是同样的原因,但没有对应的add()方法。一般嘛,就另建一个List了 下面是网上的其他解释,更能从本质上解释原因: Iterator 是工作在一个独立的线程中,并且拥有一个 mutex 锁。 Iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast 原则 Iterator 会马上抛出 java.util.ConcurrentModificationException 异常。 所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。但你可以使用 Iterator 本身的方法 remove() 来删除对象, Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。
0 0
- list中remove的问题,以及 ConcurrentModificationException异常
- list动态remove处理并发异常ConcurrentModificationException 问题
- 关于list的ConcurrentModificationException 异常
- foreach循环中remove引起java.util.ConcurrentModificationException异常
- List remove时出错"ConcurrentModificationException"
- 从ConcurrentModificationException到 list remove
- List remove中要注意的问题
- List remove中要注意的问题
- List remove中要注意的问题
- ListIterator使用时的ConcurrentModificationException异常问题
- 关于list的 java.util.ConcurrentModificationException异常处理
- list set集合remove() ConcurrentModificationException 原因
- java.util.ConcurrentModificationException 集合remove异常
- 关于java集合的遍历以及ConcurrentModificationException(并发操作异常)
- ConcurrentModificationException异常以及iterator迭代器的使用原理
- 关于List的ConcurrentModificationException
- ConcurrentModificationException 的异常分析
- ConcurrentModificationException 的异常分析
- C语言—字符串(字符数组)
- SSIS 字符串转时间格式注意事项
- Android编程之解决android-support-v4打包问题
- fbo多个color buffer绘制
- 苦楝花开,如果人生可以重来
- list中remove的问题,以及 ConcurrentModificationException异常
- Caffe安装配置
- 北邮新OJ108
- 关于CrtmpServer中的RTMP协议头处理
- MPI编程->环境配置
- Windows下本地RabbitMQ服务的安装(V3.01)
- LoadRunner牛刀小试2--实用小TIPs
- cocos2dx之Http网络编程(一)——网络请求的发送和接收
- 基于HTML5 Canvas的开源图形组件-ichartjs 入门篇