java.util.ConcurrentModificationException 解决办法 今天在项目的中有一个需求,须要在一个Set类型的集合中删除知足条件的对象,这时想固然地想到直接调用Set的remove(Object o)方法将指定的对象删除便可,测试代码: public class Test { public static void main(String[] args) { User user1 = new User(); user1.setId(1); user1.setName("zhangsan"); User user2 = new User(); user2.setId(2); user2.setName("lisi"); Set userSet = new HashSet(); userSet.add(user1); userSet.add(user2); for (Iterator it = userSet.iterator(); it.hasNext();) { User user = (User) it.next(); if (user.getId() == 1) { userSet.remove(user); } if (user.getId() == 2) { user.setName("zhangsan"); } } for(Iterator it = userSet.iterator(); it.hasNext(); ) { User user = (User) it.next(); System.out.println(user.getId() + "=>" + user.getName()); } } } 但运行程序的时候,却发现出错了: Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(Unknown Source) at java.util.HashMap$KeyIterator.next(Unknown Source) at test.Test.main(Test.java:23) 从API中能够看到List等Collection的实现并无同步化,若是在多 线程应用程序中出现同时访问,并且出现修改操做的时候都要求外部操做同步化;调用Iterator操做得到的Iterator对象在多线程修改Set的时 候也自动失效,并抛出java.util.ConcurrentModificationException。这种实现机制是fail-fast,对外部 的修改并不能提供任何保证。 网上查找的关于Iterator的工做机制。Iterator是工做在一个独立的线程中,而且拥有一个 mutex锁,就是说Iterator在工做的时候,是不容许被迭代的对象被改变的。Iterator被建立的时候,创建了一个内存索引表(单链表),这 个索引表指向原来的对象,当原来的对象数量改变的时候,这个索引表的内容没有同步改变,因此当索引指针往下移动的时候,便找不到要迭代的对象,因而产生错 误。List、Set等是动态的,可变对象数量的数据结构,可是Iterator则是单向不可变,只能顺序读取,不能逆序操做的数据结构,当 Iterator指向的原始数据发生变化时,Iterator本身就迷失了方向。 如何才能知足需求呢,须要再定义一个List,用来保存须要删除的对象: List delList = new ArrayList(); 最后只须要调用集合的removeAll(Collection con)方法就能够了。 public class Test { public static void main(String[] args) { boolean flag = false; User user1 = new User(); user1.setId(1); user1.setName("shangsan"); User user2 = new User(); user2.setId(2); user2.setName("lisi"); Set userSet = new HashSet(); userSet.add(user1); userSet.add(user2); List delList = new ArrayList(); for (Iterator it = userSet.iterator(); it.hasNext();) { User user = (User) it.next(); if (user.getId() == 1) { delList.add(user); } if (user.getId() == 2) { user.setName("zhangsan"); } } userSet.removeAll(delList); for(Iterator it = userSet.iterator(); it.hasNext(); ) { User user = (User) it.next(); System.out.println(user.getId() + "=>" + user.getName()); } } }