您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为什么会引发ConcurrentModificationException以及如何对其进行调试

为什么会引发ConcurrentModificationException以及如何对其进行调试

这不是同步问题。如果要迭代的基础集合被Iterator本身以外的任何东西修改,则会发生这种情况。

Iterator it = map.entrySet().iterator();
while (it.hasNext())
{
   Entry item = it.next();
   map.remove(item.getKey());
}

第二次调用it.hasNext()时,将抛出ConcurrentModificationException

正确的方法

   Iterator it = map.entrySet().iterator();
   while (it.hasNext())
   {
      Entry item = it.next();
      it.remove();
   }

假设此迭代器支持remove()操作。

其他 2022/1/1 18:18:12 有429人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶