java.util.ConcurrentModificationException异常情形1

出现这种异常,网上有很多中情况解释,下面是出现这种异常的另外一种方式避免的解决方案:

public static void main(String[] args) {
        Map<String, Object> map1 = Maps.newLinkedHashMap();
        map1.put("one", 11);
        map1.put("one1", 11);
        map1.put("one2", 11);
        map1.put("one3", 11);
        Map<String, Object> map2 = Maps.newHashMap();
        map2.put("one", 11);
        map2.put("one1", 11);
        map2.put("one2", 11);
        map2.put("one3", 11);
        Map<String, Object> map3 = Maps.newHashMap();
        map3.put("one", 11);
        map3.put("one1", 11);
        map3.put("one2", 11);
        map3.put("one3", 11);
        Map<String, Object> map4 = Maps.newHashMap();
        map4.put("one", 44);
        map4.put("one1", 44);
        map4.put("one2", 11);
        map4.put("one3", 11);
        List<Map<String, Object>> mapList = Lists.newArrayList();
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        mapList.add(map4);
        for (int i = 0; i < mapList.size(); i++) {
            mapList = Collections.synchronizedList(mapList);
            for (Map<String, Object> map : mapList) {
                // 对每个map插入key-value
                // 出现异常:java.util.ConcurrentModificationException,为什么需要遍历map的keySet呢?直接改成map.containsKey()就可以避免了
//                for (String mapKey : map.keySet()) {
//                    if ("one".equals(mapKey)) {
//                        map.put("111", "oneoneone");
//                    }
//                }
                if (map.containsKey("one")) {
                    map.put("111", "oneoneone");
                }
                
                // 对每个map删除key-value
                Set<Map.Entry<String,Object>> entries = map.entrySet();
                Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, Object> next = iterator.next();
                    if ("one".equals(next.getKey())) {
                        iterator.remove();
                    }
                }
            }
        }
        mapList.forEach(System.out::println);
    }

结果展示:

 

 

异常>>>>>> 出错演示:

代码演示:

public static void main(String[] args) {
        Map<String, Object> map1 = Maps.newLinkedHashMap();
        map1.put("one", 11);
        map1.put("one1", 11);
        map1.put("one2", 11);
        map1.put("one3", 11);
        Map<String, Object> map2 = Maps.newHashMap();
        map2.put("one", 11);
        map2.put("one1", 11);
        map2.put("one2", 11);
        map2.put("one3", 11);
        Map<String, Object> map3 = Maps.newHashMap();
        map3.put("one", 11);
        map3.put("one1", 11);
        map3.put("one2", 11);
        map3.put("one3", 11);
        Map<String, Object> map4 = Maps.newHashMap();
        map4.put("one", 44);
        map4.put("one1", 44);
        map4.put("one2", 11);
        map4.put("one3", 11);
        List<Map<String, Object>> mapList = Lists.newArrayList();
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        mapList.add(map4);
        for (int i = 0; i < mapList.size(); i++) {
            mapList = Collections.synchronizedList(mapList);
            for (Map<String, Object> map : mapList) {
                // 对每个map插入key-value
                // 出现异常:java.util.ConcurrentModificationException,为什么需要遍历map的keySet呢?直接改成map.containsKey()就可以避免了
                for (String mapKey : map.keySet()) {
                    if ("one".equals(mapKey)) {
                        map.put("111", "oneoneone");
                    }
                }
//                if (map.containsKey("one")) {
//                    map.put("111", "oneoneone");
//                }
                
                // 对每个map删除key-value
                Set<Map.Entry<String,Object>> entries = map.entrySet();
                Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, Object> next = iterator.next();
                    if ("one".equals(next.getKey())) {
                        iterator.remove();
                    }
                }
            }
        }
        mapList.forEach(System.out::println);
    }

 

出错异常结果:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719)
    at java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:742)
    at com.uxseeing.test.CrossAnalyticsTest.main(CrossAnalyticsTest.java:205)

 

posted @ 2019-08-29 17:40  superdrew  阅读(621)  评论(0编辑  收藏  举报