list删除操作 java.util.ConcurrentModificationException
首先大家先看一段代码:
public static void main(String[] args) {
List<String> listStr = new ArrayList<String>();
listStr.add("1");
listStr.add("2");
listStr.add("3");
listStr.add("4");
listStr.add("5");
System.out.println("listStr.size:::"+listStr.size());
for(String str:listStr){
if("3".equals(str)){
listStr.remove(str);
}
}
System.out.println("listStr.size:::"+listStr.size());
}
现象:
该程序会抛出一个 java.util.ConcurrentModificationException异常。
分析:
对 Collection 或 Map 进行迭代操作过程中尝试直接修改 Collection / Map 的内容时,会抛出java.util.ConcurrentModificationException 异常。 因为在修改数据时不能同步原有list中数据,当下一次循环时找不到数据。
解决办法:
Iterator 在工作的时候是不允许被迭代的对象被改变的,使用 Iterator 本身的方法 remove() 来删除对
象, Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。
改后代码:
public static void main(String[] args) {
List<String> listStr = new ArrayList<String>();
listStr.add("1");
listStr.add("2");
listStr.add("3");
listStr.add("4");
listStr.add("5");
System.out.println("listStr.size:::"+listStr.size());
Iterator<String> ite = listStr.iterator();
while(ite.hasNext()){
String str = (String)ite.next();
if("3".equals(str)){
ite.remove();
}
}
System.out.println("listStr.size:::"+listStr.size());
}
补充:
或许有人可能会问如果我不用增强for循环,直接用.get(index)方法会不会报错?
代码如下:
for(int i = 0 ; i < listStr.size(); i++){
if("3".equals(listStr.get(i))){
listStr.remove(i);
}
}
现象:
listStr.size:::5
listStr.size:::4
大家可以看到通过list的下表索引来修改list数据是不会出错的。
分析:
大家可以这样认为:通过所引来循环时,jvm记录的是所引值,当移除当前对象后,其他元素的索引号不会同步改变。下次循环仍可以找到对应数据。而增强for循环,记录的是当前对象,当下次循环时,会先找到该对象,然后游标向下移,这时候找不到对象所以结果会混乱。
java.util.ConcurrentModificationException
【引言】
经常在迭代集合元素时,会想对集合做修改(add/remove)操作,类似下面这段代码:
- for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {
- Integer val = it.next();
- if (val == 5) {
- list.remove(val);
- }
- }
运行这段代码,会抛出异常java.util.ConcurrentModificationException。
【解惑】
(以ArrayList来讲解)在ArrayList中,它的修改操作(add/remove)都会对modCount这个字段+1,modCount可以看作一个版本号,每次集合中的元素被修改后,都会+1(即使溢出)。接下来再看看AbsrtactList中iteraor方法
- public Iterator<E> iterator() {
- return new Itr();
- }
它返回一个内部类,这个类实现了iterator接口,代码如下:
- private class Itr implements Iterator<E> {
- int cursor = 0;
- int lastRet = -1;
- int expectedModCount = modCount;
- public boolean hasNext() {
- return cursor != size();
- }
- public E next() {
- checkForComodification();
- try {
- E next = get(cursor);
- lastRet = cursor++;
- return next;
- } catch (IndexOutOfBoundsException e) {
- checkForComodification();
- throw new NoSuchElementException();
- }
- }
- public void remove() {
- if (lastRet == -1)
- throw new IllegalStateException();
- checkForComodification();
- try {
- AbstractList.this.remove(lastRet);
- if (lastRet < cursor)
- cursor--;
- lastRet = -1;
- // 修改expectedModCount 的值
- expectedModCount = modCount;
- } catch (IndexOutOfBoundsException e) {
- throw new ConcurrentModificationException();
- }
- }
- final void checkForComodification() {
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- }
- }
在内部类Itr中,有一个字段expectedModCount ,初始化时等于modCount,即当我们调用list.iterator()返回迭代器时,该字段被初始化为等于modCount。在类Itr中next/remove方法都有调用checkForComodification()方法,在该方法中检测modCount == expectedModCount,如果不相当则抛出异常ConcurrentModificationException。
前面说过,在集合的修改操作(add/remove)中,都对modCount进行了+1。
在看看刚开始提出的那段代码,在迭代过程中,执行list.remove(val),使得modCount+1,当下一次循环时,执行 it.next(),checkForComodification方法发现modCount != expectedModCount,则抛出异常。
【解决办法】
如果想要在迭代的过程中,执行删除元素操作怎么办?
再来看看内部类Itr的remove()方法,在删除元素后,有这么一句expectedModCount = modCount,同步修改expectedModCount 的值。所以,如果需要在使用迭代器迭代时,删除元素,可以使用迭代器提供的remove方法。对于add操作,则在整个迭代器迭代过程中是不允许的。 其他集合(Map/Set)使用迭代器迭代也是一样。
//获奖显示
public String prizeList(){
voteList = userWorksService.getAllRank();
UserWorks userWorks = new UserWorks();
Iterator<UserWorks> list = voteList.iterator();
Integer rank ;
while(list.hasNext()){
userWorks = list.next();
if(!("9".equals(userWorks.getAwardsRank()))){
rank = Integer.parseInt(userWorks.getAwardsRank())+2;
userWorks.setAwardsRank(rank +"");
allList.add(userWorks);
}
}
voteList.removeAll(allList);
return "prizeList";
}
list删除操作 java.util.ConcurrentModificationException的更多相关文章
- 再次踩bug:遍历删除list(java.util.ConcurrentModificationException)
再次踩bug:遍历删除list(java.util.ConcurrentModificationException) 使用 List<Long> list = new ArrayList& ...
- 为什么阿里巴巴禁止在 foreach 循环里进行元素的 remove/add 操作--java.util.ConcurrentModificationException
摘要 foreach循环(Foreach loop)是计算机编程语言中的一种控制流程语句,通常用来循环遍历数组或集合中的元素. 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体 ...
- 遍历List过程中删除操作报java.util.ConcurrentModificationException错误
1:遍历List 同时 remove 元素,出现java.util.ConcurrentModificationException错误 @Test public void test3(){ List& ...
- 对ArrayList操作时报错java.util.ConcurrentModificationException null
用iterator遍历集合时要注意的地方:不可以对iterator相关的地方做添加或删除操作.否则会报java.util.ConcurrentModificationException 例如如下代码: ...
- Hashtable 删除元素, 抛出异常 java.util.ConcurrentModificationException
今天在对一个Hashtable对象进行 搜索 -> 删除 操作时遇到的一个问题,开始的使用我使用的是Hashtable的Iterator,然后直接执行: Hashtable.remove(key ...
- 集合循环删除问题-报错java.util.ConcurrentModificationException解析
java.util.ConcurrentModificationException 异常问题详解 环境:JDK 1.8.0_111 在Java开发过程中,使用iterator遍历集合的同时对集合进行修 ...
- JAVA循环迭代中删除或添加集合数据报java.util.ConcurrentModificationException错误
1.写出下面的输出结果 public class test{ public static void main(String [] args) List<String> list = new ...
- SpringMvc中Hashmap操作遇到 java.util.ConcurrentModificationException: null
代码按照网上修改为类似,还不能解决问题 for (Iterator<String> it = target.keySet().iterator(); it.hasNext(); ) { i ...
- java.util.ConcurrentModificationException 解决办法(转载)
今天在项目的中有一个需求,需要在一个Set类型的集合中删除满足条件的对象,这时想当然地想到直接调用Set的remove(Object o)方法将指定的对象删除即可,测试代码: public cla ...
随机推荐
- Windows Live Writer测试插件
这是用WLW写的,测试 这是图片: 这是地图: 这是代码: 一会重发
- Mysql,SqlServer,Oracle主键自动增长的设置
1.把主键定义为自动增长标识符类型 MySql 在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: )); insert into customers ...
- Spring学习之AOP
Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...
- HDU 5389 Zero Escape
题意:有一些人,每人拿一个号码,有两个门,门的值分别为A和B,要求把人分成两堆(可以为空)一堆人手持号码之和的数字根若等于A或者B就可以进入A门或者B门,要求两堆人分别进入不同的门,求有几种分配方式, ...
- C# chart,有关如何在鼠标移动到Series上时显示节点及数据 (有待继续更新)
一.效果与思路 效果: 解决方案1 用chart的mousemove时间,实时跟踪鼠标最近的X轴的位置,然后把cursorX设置到那个位置上,让用户知道我是选的那一个X的值,同时用tooltip显示该 ...
- adb remount 失败remount failed: Operation not permitted
1. 进入shell adb shell 2. shell下输入命令 shell@android:/ $ sushell@android:/ # mount -o rw,remount -t yaff ...
- java web 学习六(servlet开发2)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- Redis入门教程:特性及数据类型的操作
虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了解并不全面,下面是一个比较系统的Redis介绍,对Redis的特性及各种数据类型及操作进行了介绍.是一个很不错的Redis入门 ...
- duilib底层机制剖析:窗体类与窗体句柄的关联
转载请说明原出处,谢谢~~ 看到群里朋友有人讨论WTL中的thunk技术,让我联想到了duilib的类似技术.这些技术都是为了解决c++封装的窗体类与窗体句柄的关联问题. 这里是三篇关于thunk技术 ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...