java.util.ConcurrentModificationException

错误原因:在使用迭代器过程中对迭代对象进行操作导致

 示例代码:

 1 Map<String,String> map=new HashMap<String,String>();
 2         map.put("a", "aa");
 3         map.put("b", "bb");
 4         map.put("c", "cc");
 5         map.put("d", "dd");
 6         for(String str:map.keySet()){
 7             if("c".equals(str)){
 8                 map.remove(str);
 9             }
10         }

这段代码会抛出下面的异常,就是因为在迭代的时候第8行进行了移除操作。

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
at com.test.MapTest.main(MapTest.java:13)

原文地址:https://www.cnblogs.com/ssh2/p/3711961.html