2019独角兽企业重金招聘Python工程师标准>>>

http://stackoverflow.com/questions/8104692/how-to-avoid-java-util-concurrentmodificationexception-when-iterating-through-an

Two options:

  • Create a list of values you wish to remove, adding to that list within the loop, then call originalList.removeAll(valuesToRemove) at the end
  • Use the remove() method on the iterator itself. Note that this means you can't use the enhanced for loop.

As an example of the second option, removing any strings with a length greater than 5 from a list:

List<String> list = new ArrayList<String>();
...
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {String value = iterator.next();if (value.length() > 5) {iterator.remove();}
}

转载于:https://my.oschina.net/u/2308739/blog/824381

java.util.ConcurrentModificationException when interation the list then remove相关推荐

  1. 循环体中对集合进行增删时报错:java.util.ConcurrentModificationException

    先看一段代码: @Testpublic void test02(){List<String> list = new ArrayList<>(8);list.add(" ...

  2. java.util.ConcurrentModificationException(并发修改错...

    为什么80%的码农都做不了架构师?>>>    public class ConcurrentModificationExceptionextends RuntimeExceptio ...

  3. 出现 java.util.ConcurrentModificationException 时的解决办法

    for (int i=0; i<list.size(); i++) { // 只有在这种方式遍历集合时,同时对集合增加.删除才不会影响. // list.remove(list.get(i)); ...

  4. java.util.ConcurrentModificationException异常原因及解决方法

    java.util.ConcurrentModificationException异常原因及解决方法 参考文章: (1)java.util.ConcurrentModificationExceptio ...

  5. 非线程安全类ArrayList出现异常:java.util.ConcurrentModificationException

    今天执行了一段<图解多线程设计模式>中的代码,结果抛出了如下的异常: Exception in thread "ReaderThread" java.util.Conc ...

  6. 异常:java.util.ConcurrentModificationException

    Quote: 异常:java.util.ConcurrentModificationException 在Map或者Collection的时候,不要用它们的API直接修改集合的内容,如果要修改可以用I ...

  7. java.util.ConcurrentModificationException 解决办法

    在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除. 则使用会报以下异常: Java.util.ConcurrentModifi ...

  8. List中subList方法抛出异常java.util.ConcurrentModificationException原理分析

    1.首先从测试代码开始: public class Test {public static void main(String[] args) {List<Integer> list = n ...

  9. 为什么会出现 java.util.ConcurrentModificationException 异常?

    http://www.iteye.com/topic/124788 在Map或者Collection的时候,不要用它们的API直接修改集合的内容(否则会出现 java.util.ConcurrentM ...

最新文章

  1. 极客新闻——17、给决定长久做程序员的你三个建议
  2. [转]淘宝下单高并发解决方案
  3. 客户端跳转代码html5,HTML5跳转小程序wx-open-launch-weapp的示例代码
  4. 线程同步之互斥量加锁解锁 死锁
  5. JVM学习笔记-03-类加载器及双亲委派机制
  6. c# mysql 操作_c#对mysql数据库的基本操作
  7. 关于MX5在Mac无法正常使用开发者模式的解决方案
  8. STM32F103DAC功能实现
  9. mysql 5.7 编译_MySQL5.7.20编译安装
  10. 三相同步电机怎么接线图_实用!41例电动机接线方法-清晰大图(上)
  11. 改led背光有光斑_为什么把车灯改成LED,效果还不如卤素?
  12. 中标麒麟怎么安装deb包_银河麒麟制作deb安装包
  13. linux系统发生假死状态处理办法
  14. Vue设置浏览器title-icon
  15. 学习如何使用 Git 来压扁、变基和精选
  16. bubble pop
  17. 机器学习分类算法之XGBoost(集成学习算法)
  18. 【程序喵笔记】递归神经网络实现预测
  19. 如何做好企业网站优化与推广工作
  20. eas java配置_EAS客户端调试环境搭建

热门文章

  1. 《Flutter 控件大全》第五十五个:InkWell和Ink
  2. 【剑指offer】一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
  3. python 柱状图如何添加数字标签_matplotlib可视化之如何给图形添加数据标签?
  4. 企业风险管理的四种模式五种策略分别是什么?
  5. FPGA学习日记(八)SDRAM的读写测试
  6. Linux用户对文件进行加密
  7. springboot微信点餐系统项目设计
  8. 关于netty的IllegalReferenceCountException refCnt: 0, decrement: 1
  9. Windows10下载安装docker
  10. linux-uos输入法丢失解决办法