Java 迭代器删除元素ConcurrentModificationException异常。
Java是不支持容器类在使用迭代器迭代过程中,使用如 list.remove(obj)方法删除元素。否则会抛出ava.util.ConcurrentModificationException异常。应该使用iterator.remove()方法删除当前迭代到的元素。
这是因为Java集合中有一种叫fail-fast的机制,即如果多个线程对同一个集合的内容进行操作时,则会产生fail-fast事件,即抛出异常。
比如下面代码
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Hello {
public static void main(String[] args) {
List<String> all=new ArrayList<String>();
all.add("Hello");
all.add("_");
all.add("World!!");
Iterator<String> iterator=all.iterator();
while(iterator.hasNext())
{
String str=iterator.next();
if("Hello".equals(str))
{
all.remove(str);
}
else
{
System.out.println( str+" ");
}
}
System.out.println("\n删除\"_\"之后的集合当中的数据为:"+all);
}
}
删除元素会抛出下面异常

这里在迭代过程中改变了原来集合的存储内容,因此出发了fail-fast机制。
fail-fast机制是这样设置的。以上面代码抛出的异常为例。查看ArrayList.类的源码,可以看到抛出异常的函数为

这里的modCount文档是这样解释的
The number of times this list has been structurally modified. Structural modifications are those that change the size of the list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.
大概意思就是list的结构被修改的次数。结构的修改包括改变list的大小,或者其他可能使迭代结果产生错误的干扰行为。
expectedModCount注释为:
The modCount value that the iterator believes that the backing List should have. If this expectation is violated, the iterator has detected concurrent modification.
因此若是在迭代过程中改变了List的结构,即使用了remove,或者等add方法则会导致expectedModCount的值与modCount的值不同,就会抛出异常。
但是有一个奇特的现象,如果我们使用List.remove(obj)方法删除倒数第二个元素则不会超出异常。如
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Hello {
public static void main(String[] args) {
List<String> all=new ArrayList<String>();
all.add("Hello");
all.add("_");
all.add("World!!");
Iterator<String> iterator=all.iterator();
while(iterator.hasNext())
{
String str=iterator.next();
if("_".equals(str))
{
all.remove(str);
}
else
{
System.out.println( str+" ");
}
}
System.out.println("\n删除\"_\"之后的集合当中的数据为:"+all);
}
}
注意这里删除的'_'为集合中的倒数第二个元素。同时注意代码有,若没有删除此元素,则输出此元素,即else里面的内容。结果为

不是说,不能用List.remove(obj)的方式,只能用iterator.remove()的方式删除,这里为什么没有抛异常。原因是这样。
看ArrayList的hasNext()方法与next()方法的源码
size为当前集合的大小,cursor为下个元素的索引,初始化为0

其中checkForComodification的源码为,两个变量的的解释上面也已经说了。

我们的迭代器部分代码按下面步骤执行:
all里面存储了“Hello”,"_","world!"三个元素
- iterator.hasNext(),
- cursor=0,size=3。cursor!=size,返回true,进入while内
- String str=iterator.next(),
- checkForComodification(),此时modCount=0,expectedModCount=0,因此没有抛出异常,
- cursor=i+1,即cursor=1
- return elementData[lastRet=i],str='Hello'
- '_'.equals(str)为False,输出Hello
- iterator.hasNext(),
- cursor=1,size=3。cursor!=size,返回true,进入while内
- String str=iterator.next(),
- checkForComodification(),此时modCount=0,expectedModCount=0,因此没有抛出异常,
- cursor=i+1,即cursor=2
- return elementData[lastRet=i],str='_'
- '_'.equals(str)为Ture,
- all.remove(str)
- modCount=1,因为改变了List的结构,因此modCount+1。
- iterator.hasNext(),
- cursor=2,size=2。cursor!=size,返回False,退出
可以看到,在这里就已经推出while循环了。不会在执行checkForComodification()方法,判断List结构有没有改变。
结论:因此只要对倒数第二个元素执行remove()方法,虽然会改变集合结构,但是因为也同时改变了集合size,使size与cursor的大小一样,就会直接循环,不会报异常。
Java 迭代器删除元素ConcurrentModificationException异常。的更多相关文章
- 集合遍历过程iterator, 添加删除元素报异常
list set 遍历过程中添加或者删除元素,报异常. 使用iterator 也会报异常 ConcurrentModificationException remove只能用迭代器的remove,而 ...
- STL 中 使用迭代器删除元素的问题
在vector中删除,大家都知道,直接erase的话,这种写法很有问题.因为erase(iter)之后iter指针就变成野指针了,此时继续iter++就会出问题. for(auto iter = v. ...
- C++中利用迭代器删除元素会发生什么?
转自:https://blog.csdn.net/yf_li123/article/details/75003425#comments (1)对于关联容器(如map,set,multimap,mu ...
- 该文档举例说明了multimap的查找和删除元素的使用
该文档举例说明了multimap的查找和删除元素的使用. 其中,在使用迭代器遍历元素的时候,如果使用了删除迭代器的操作,那么需要小心迭代器失效的情况. /* 功能说明: multimap的查找和删除元 ...
- Java中集合删除元素时候关于ConcurrentModificationException的迷惑点
下面的示例来至于阿里巴巴Java开发手册的集合处理部分的第7条: 运行如下代码,会发现正确运行. public static void hasNotExcption() { List<Strin ...
- Java循环删除集合多个元素的正确打开方式
首先说下不正确的打开方式: 第一:使用for循环删除集合的元素,示例代码如下 ArrayList<String> list = new ArrayList<String>(Ar ...
- Java ConcurrentModificationException异常原因和解决方法
Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...
- Java并发编程:Java ConcurrentModificationException异常原因和解决方法
Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...
- Java HashMap 如何正确遍历并删除元素
(一)HashMap的遍历 HashMap的遍历主要有两种方式: 第一种采用的是foreach模式,适用于不需要修改HashMap内元素的遍历,只需要获取元素的键/值的情况. HashMap<K ...
随机推荐
- http://blog.csdn.net/sdksdk0/article/details/50749326
http://blog.csdn.net/sdksdk0/article/details/50749326
- 我的常用的Linux命令
环境:centos7 主要应用Linux命令是为了搭建环境,所以记录一下我的常用的Liunx命令 一.常用目录.文件操作命令 1.显示目录列表命令 ls 显示当前目录下的可见文件 ls - ...
- 【ABAP系列】SAP 关于出口(user-exit)MV50AFZ1的一些问题
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 关于出口(user-ex ...
- 好的计数思想-LightOj 1213 - Fantasy of a Summation
https://www.cnblogs.com/zhengguiping--9876/p/6015019.html LightOj 1213 - Fantasy of a Summation(推公式 ...
- 工具 - MSF
#ms17- use auxiliary/scanner/smb/smb_ms17_010 - exploit use exploit/windows/smb/ms17_010_eternalblue ...
- vue 常用插件,保存
UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 mint-ui- Vue 2的移动UI元素 iview- 基于 Vuejs 的开源 UI ...
- python中pycharm中.py文件调用一个.py文件的函数
在相同文件夹内调用函数: file1.py def add(x,y): print('和为:%d'%(x+y)) file2.py import A A.add(1,2)
- ES6 new Set实现数组去重
使用new Set实现数组去重必须结合for of, 如果使用for循环就实现不了 var arr = new Set([1, 2, 1, 1, 2, 3, 3, 4, 4]); for (var e ...
- 在a标签中使用了onclick修改样式之后a:hover失效
是因为优先级的原因造成,使用!important修改优先级. 如修改成: .button1:hover { color: #FFF !important; ...
- qemu-kvm使用
创建镜像qemu-img create -f qcow2 test-vm.qcow2 10g 修改镜像大小qemu-img resize test-vm.qcow2 +10G 安装系统 qem ...