Java中的Iterator功能比较简单,并且只能单向移动:

  (1) 使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的next()方法时,它返回序列的第一个元素。注意:iterator()方法是java.lang.Iterable接口,被Collection继承。

  (2) 使用next()获得序列中的下一个元素。

  (3) 使用hasNext()检查序列中是否还有元素。

  (4) 使用remove()将迭代器新返回的元素删除。

迭代器应用:
           list l = new ArrayList();
           l.add("aa");
           l.add("bb");
           l.add("cc");
           for (Iterator iter = l.iterator(); iter.hasNext();) {
                      String str = (String)iter.next();
                      System.out.println(str);
           }
 /*迭代器用于while循环
           Iterator iter = l.iterator();
           while(iter.hasNext()){
                      String str = (String) iter.next();
                      System.out.println(str);
           }
 */

Iterator升级

在AbstractList中还有一个内部类Itr,Itr implements Iterator,Itr是一个List遍历的工具类,当然list.iterator()方法也是返回Itr对象,在Itr中有一个校验位属性expectedModCount,对于一个Itr对象,其初始时expectedModCount=modCount。
【注】在AbstractList中,有一个属性modCount,这个属性是跟踪List中数据被修改的次数,任何对List的add/remove操作,都将导致modCount++。
Iterator是List一个视图,其最终还是操作List的存储结构。在使用iterator遍历时,remove()操作,会导致modCount++,因为有expectedModCount=modCount,即在 iterator中remove数据,会带来expectedModCount与modCount值的同步。
在Iterator遍历时,next(),remove()方法会校验expectedModCount与modCount值是否一致,如果不一致,就意味着这List数据在iterator外部被修改,此时iterator遍历将会造成 ConcurrentModificationException。
AbstractList不仅支持普通的Iterator,还支持ListIterator(ArrayList,LinkedList均支持),ListIterator增加了遍历时双向游标能力(previous,next),增加了add方法。add方法和remove方法一样也做了expectedModCount和modCount一致性校验。
 
 
 
 

在单线程操作的情况下,在DAO层查询到数据集合后,返回到service层做业务处理,要求:遍历数据集合,判断不符合条件的元素,做删除操作。

在用foreach和 Iterator 都会发生java.util.ConcurrentModificationException。

看一下JavaDoc对java.util.ConcurrentModificationException异常的描述:
当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。

查看源码后终于发现了原因是因为:

迭代器的modCount和expectedModCount的值不一致。

单线程中该异常出现的原因是:对一个集合遍历的同时,有对该集合进行了增删的操作。导致AbstarctList的modCount和expectedModCount的值不一致。

而我们要做的就是将需要操作的元素放到中间元素中,并记录操作标志位。在遍历结束后进行增删操作。

或自定义迭代器复写其中的相关操作,在操作结束后添加expectedModCount = modCount;

多线程中更容易出现该异常,当你在一个线程中对一数据集合进行遍历,正赶上另外一个线程对该数据集合进行增删操作。

解决方案:
1)在使用iterator迭代的时候使用synchronized或者Lock进行同步;

2)使用并发容器CopyOnWriteArrayList代替ArrayList和Vector。

以下是Demo:推荐大家使用for循环进行遍历集合,在for循环中做增删操作。

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; public class Test { // 出现java.util.ConcurrentModificationException
public List<String> m1(List<String> list) {
for (String temp : list) {
if ("3".equals(temp)) {
list.remove(temp);
}
}
return list; }
// 出现java.util.ConcurrentModificationException
public List<String> m2(List<String> list) {
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String temp = iterator.next();
if ("3".equals(temp)) {
list.remove(temp);
} }
return list; }
//successful!
public List<String> m3(List<String> list) {
for (int i = 0; i < list.size(); i++) {
if ("3".equals(list.get(i))) {
list.remove(i);
}
}
return list; }
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
Test test = new Test();
List<String> listTemp = test.m1(list);
System.out.println(listTemp.toString());
}
}

Iterator 在工作的时候是不允许被迭代的对象被改变的。

但你可以使用 Iterator 本身的方法 remove() 来删除对象,Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。

 

正确的在遍历的同时删除元素的姿势:

Iterator<Student> stuIter = students.iterator();
while (stuIter.hasNext()) {
Student student = stuIter.next();
if (student.getId() == 2)
stuIter.remove();//这里要使用Iterator的remove方法移除当前对象,如果使用List的remove方法,则同样会出现ConcurrentModificationException
}

参考:https://mp.weixin.qq.com/s?__biz=MzI3NzE0NjcwMg%3D%3D&mid=2650121134&idx=1&sn=a34a1bd547f00e479e9f6dbde8848fe4&chksm=f36bbe8fc41c3799d1bb2c781f81f51e28651d2fb8eb5670a31caac5ba782b66416e5fdf1b1c&mpshare=1&scene=23&srcid=0414ouzb2yYypPWh2K0QVhtY%23rd

增强for循环 java.util.ConcurrentModificationException的更多相关文章

  1. 为什么阿里巴巴禁止在 foreach 循环里进行元素的 remove/add 操作--java.util.ConcurrentModificationException

    摘要 foreach循环(Foreach loop)是计算机编程语言中的一种控制流程语句,通常用来循环遍历数组或集合中的元素. 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体 ...

  2. 集合循环删除问题-报错java.util.ConcurrentModificationException解析

    java.util.ConcurrentModificationException 异常问题详解 环境:JDK 1.8.0_111 在Java开发过程中,使用iterator遍历集合的同时对集合进行修 ...

  3. java foreach循环抛出异常java.util.ConcurrentModificationException

    代码如下: for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) { if (Integer.parseInt(i ...

  4. JAVA循环迭代中删除或添加集合数据报java.util.ConcurrentModificationException错误

    1.写出下面的输出结果 public class test{ public static void main(String [] args) List<String> list = new ...

  5. LinkedList - java.util.ConcurrentModificationException

    package com.test.io; import java.io.BufferedReader; import java.io.FileNotFoundException; import jav ...

  6. list删除操作 java.util.ConcurrentModificationException

    首先大家先看一段代码: public static void main(String[] args) { List<String> listStr = new ArrayList<S ...

  7. Java Bug -- java.util.ConcurrentModificationException

    java.util.ConcurrentModificationException at java.util.ArrayList$ArrayListIterator.next(ArrayList.ja ...

  8. 浅谈java.util.ConcurrentModificationException(并发修改异常)

    java中的list集合是我们经常使用的集合,而对集合进行增加和删除元素是我们最常用的操作.那么在什么时候对list集合什么样的操作,就会发生java.util.ConcurrentModificat ...

  9. 再次踩bug:遍历删除list(java.util.ConcurrentModificationException)

    再次踩bug:遍历删除list(java.util.ConcurrentModificationException) 使用 List<Long> list = new ArrayList& ...

随机推荐

  1. 合唱团---DP

    https://www.nowcoder.com/practice/661c49118ca241909add3a11c96408c8?tpId=85&tqId=29830&tPage= ...

  2. vim(一) vim与markdown

    vim markdown 配置 vim高亮显示Markdown语法 在.vimrc添加 Plugin 'godlygeek/tabular' Plugin 'plasticboy/vim-markdo ...

  3. C# MODBUS 通信

    背景 电厂有多组监控设备,需要在指定的设备上显示某些数据(其他设备对接过来的).通信协议是modbus主从结构. 源码: http://download.csdn.net/download/wolf1 ...

  4. 【3dsMax安装失败,如何卸载、安装3dMax 2010?】

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  5. 安装clouderamaner时出现Failed to detect distribution错误(在ubuntu14.04版本里)

    不多说,直接上干货! 在安装过程中,本来我的ubuntu系统都是14.04的.  问题详情 问题查看 解决办法 然后,再次执行安装即可. 欢迎大家,加入我的微信公众号:大数据躺过的坑        人 ...

  6. OpenGL进阶之Instancing

    Instancing Instancing绘制我想很多童鞋都不陌生,这个技术主要用来快速渲染大量相同的几何体,可以大大提高绘制效率.每个instance在shader中都有一个独一无二的索引,可以用来 ...

  7. [linux] uptime 命令中关于平均负载的解释

    1.当前时间 00:13:25 2.系统已运行的时间 9小时19分 3.当前在线用户 2 user 4.平均负载:0.17, 0.12, 0.07 最近1分钟.5分钟.15分钟系统的负载 为了更好地理 ...

  8. 简单的一个php验证登陆代码

    <?php/** */ if ( !isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER ...

  9. python中时间对象生成及时间格式的转换

    1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 import time timeArray = time.strpt ...

  10. angular2 遇到的问题汇总

    angular2 学习资源集锦:https://github.com/timjacobi/angular2-education 在学习angular开发项目过程遇到的问题: 1. 不同componen ...