`
kabike
  • 浏览: 598618 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

增强for循环里操作集合抛出ConcurrentModificationException

阅读更多
自定义了一个简单的Student类,发现如下方法会抛出一个
java.util.ConcurrentModificationException

		List<Student> studentList = new ArrayList<Student>();
		Student s = new Student();
		s.setId(1);
		s.setName("student1");
		studentList.add(s);

		s = new Student();
		s.setId(2);
		s.setName("student2");
		studentList.add(s);

		s = new Student();
		s.setId(3);
		s.setName("student3");
		studentList.add(s);

		for (Student student : studentList) {
			if (student.getId().equals(1)) {
				studentList.remove(s);
			}
		}


这是因为增强for循环实际上还是使用了List的Iterator,而Iterator是快速失败的,即在Iterator进行迭代的过程中不能改变Collection的结构,所以不能在迭代的过程中添加或者删除元素.

要避免这个问题可以使用Iterator自带的remove方法
Iterator<Student> iterator = studentList.iterator();
		while (iterator.hasNext()) {
			if (iterator.next().getId().equals(id)) {
				iterator.remove();
			}
		}


或者重写Student的equals方法
Student studentToDelete = new Student();
		studentToDelete.setId(1);
		studentList.remove(studentToDelete);

不过后者的限制是只移除第一个符合条件的元素
分享到:
评论

相关推荐

    java 集合并发操作出现的异常ConcurrentModificationException

    Map在遍历时候通常 现获得其键值的集合Set,然后用迭代器Iterator来对Map进行遍历。

    java.util.ConcurrentModificationException 异常问题详解1

    java.util.ConcurrentModificationException 异常问题详解1

    java集合-CopyOnWriteArraySet的使用

    只读迭代:由于写时复制的机制,CopyOnWriteArraySet 的迭代器是只读的,即迭代过程中不会抛出 ConcurrentModificationException 异常。但是,迭代器获得的数据可能不包含最新的修改。 较高的内存占用:由于每次...

    多线程中使用Java集合类

    Java集合类中,某个线程在 Collection 上进行迭代时...  因此,当一个线程试图ArrayList的数据的时候,另一个线程对ArrayList在进行迭代的,会出错,抛出ConcurrentModificationException。  比如下面的代码:  

    gradle-4.8.1-all.zip 快速下载

    3、在使用 project.tasks.withType()时,Gradle 4.8 有时会失败,并抛出 ConcurrentModificationException 。 4、依赖关系解析引擎有时会抛出 “Unexpected parent dependency” 报错,这在 Gradle 4.8 中变得更加...

    Java开发常见问题总结.docx

    避免在循环中修改集合,可能导致ConcurrentModificationException。 异常处理: 不要忽视异常,合理捕获并处理它们。 不要过度使用try-catch,应尽量抛出业务异常给上层处理。 使用finally块进行资源清理。 并发...

    遍历并批量删除容器中元素出现ConcurrentModificationException原因及处置

    NULL 博文链接:https://chenlinbo.iteye.com/blog/832335

    ArrayList.java

    iterator和listIterator方法是快速失败的 :如果列表在任何时间从结构上修改创建迭代器之后,以任何方式,除了通过迭代器自身的remove或add方法,迭代器都将抛出ConcurrentModificationException 。 因此,在并发的...

    java8集合源码-zinc-ConcurrentModificationException:锌并发修改异常

    java8集合源码sbt run 锌抛: [info] running example.MainClass [info] compiling 1 Java source to C:\Users\Chikei\AppData\Local\Temp\zincClasses336102448129471145 ... [error] ## Exception when compiling ...

    解析Java的迭代器中的fast-fail错误检测机制

    那么线程A访问集合时,就会抛出ConcurrentModificationException异常,产生fail-fast事件。 fail-fast 机制是java集合(Collection)中的一种错误机制。当多个线程对同一个集合的内容进行操作时,就可能会产生fail-...

    java.util.ConcurrentModificationException 解决方法

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

    出现java.util.ConcurrentModificationException 问题及解决办法

    主要介绍了出现java.util.ConcurrentModificationException 问题及解决办法的相关资料,需要的朋友可以参考下

    fail-safe fail-fast知多少

    这时候我们就要用到Iterator,经常写程序的朋友应该都知道,在Iterator遍历的过程中,是不能够修改集合数据的,否则就会抛出ConcurrentModificationException。 因为ConcurrentModificationException的存在,就把...

    java7hashmap源码-Rebuild-Java:再度重修JAVA

    迭代时被修改抛出ConcurrentModificationException异常 迭代时集合被修改不抛出异常 使用原集合遍历集合元素 使用原集合的副本遍历集合元素 迭代器不要求额外的内存 迭代器需要额外的内存克隆集合对象 示例:...

    Java集合教程吐血整理干货.md

    java集合 线程不安全的集合 HashMap的特点 HashMap的长度(容量)为什么要设计成2的幂? HashTable的特点 TreeMap ArrayList的特点 Vector的特点 LinkedList的特点 Set ConcurrentModificationException异常 线程...

    实现Java删除一个集合的多个元素

    Java中的For each实际上使用的是iterator进行处理的。而iterator是不允许集合在...而我在for each时,从集合中删除了一个元素,这导致了iterator抛出了ConcurrentModificationException,下面来看看到底怎么回事。

    Java源码解析ArrayList及ConcurrentModificationException

    今天小编就为大家分享一篇关于Java源码解析ArrayList及ConcurrentModificationException,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    axis1.4.1.zip

    axis1.4补丁包,解决jdk1.8高并发报ConcurrentModificationException问题,该jar包重新编译jar包的一个class文件,线上环境通过

    软件测试专业术语对照表

    这里面包含了大部分的软件测试的专业术语,希望对你有用

Global site tag (gtag.js) - Google Analytics