zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    Java单例模式
    Java中子类覆盖父类方法所必须满足的条件
    Java中break、continue及标签等跳转语句的使用[下]
    CSS控制图片显示区域
    rabbitmq 用户管理
    rabbitmq web管理页面无法访问
    centos6.5 以 zero-dependency Erlang from RabbitMQ 搭建环境
    docker 解决:Get http:///var/run/docker.sock/v1.19/version: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?
    解决:java.lang.ArrayIndexOutOfBoundsException: 160 at com.alibaba.fastjson.serializer.SerializeWriter.writeStringWithDoubleQuote(SerializeWriter.java:868)
    centos 解决:Another app is currently holding the yum lock; waiting for it to exit
  • 原文地址:https://www.cnblogs.com/superdrew/p/11431090.html
Copyright © 2011-2022 走看看