遍历Map时删除元素报错问题
问题:遍历时直接remove报异常 测试代码: Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); for (String key : map.keySet()) { if ("b".equals(key)) { map.remove(key); } } 运行时报错: j a v a . u t i l . C o n c u r r e n t M o d i f i c a t i o n E x c e p t i o n 原因排查 使用增强for循环时,底层其实使用的是 Iterator。 ...