• Java Map在遍历过程中删除元素


    map遍历判断筛选删除时

    如果对map使用put、remove或clear方法(例如map.remove直接删除),那么迭代器就不再合法(并且在其后使用该迭代器将会有ConcurrentModificationException异常被抛出).

    当Iterator.remove方法导致map发生变化时,他会更新cursor来同步这一变化。

    参见jdk文档描述:

    The iterators returned by all of this class’s “collection view methods” are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

    结论: 应该使用迭代删除

    推广

    针对其他list等集合,遍历过程中的删除操作,也需要使用迭代删除

    测试demo

    private static Map map = new HashMap();

    public static void iterTest() {

    map.put(1, “one”);

    map.put(2, “two”);

    map.put(3, “three”);

    map.put(4, “four”);

    map.put(5, “five”);

    map.put(6, “six”);

    map.put(7, “seven”);

    map.put(8, “eight”);

    map.put(5, “five”);

    map.put(9, “nine”);

    map.put(10, “ten”);

    Iterator> iter = map.entrySet().iterator();

    while (iter.hasNext()) {

    Map.Entry entry = iter.next();

    int key = entry.getKey();

    if (key % 2 == 1) {

    System.out.println("delete this: " + key + " = " + key);

    //map.put(key, “奇数”); //ConcurrentModificationException

    //map.remove(key); //ConcurrentModificationException

    iter.remove(); //OK

    }

    }

    //遍历当前的map;这种新的for循环无法修改map内容,因为不通过迭代器。

    System.out.println(“------- 最终的map的元素遍历:”);

    for (Map.Entry entry : map.entrySet()) {

    int k = entry.getKey();

    String v = entry.getValue();

    System.out.println(k + " = " + v);

    }

    }

    在main方法中运行 iterTest() ,输出结果为:

    -------

    最终的map的元素遍历:

    2 = two

    4 = four

    6 = six

    8 = eight

    10 = ten

    若将

    iter.remove();

    替换成

    map.put(key, “奇数”);

    或者 map.remove(key);

    则会报出 ConcurrentModificationException 异常

  • 相关阅读:
    涨点神器!全局注意力+位置注意力,打造更强深度学习模型
    C++常量
    Android四大组件之- Service的创建与应用
    你真的了解CPU和GPU?
    慢 SQL 分析与优化
    【数据结构练习题】二叉树(1)——1.相同的树2.另一颗树的子树3.翻转二叉树4.平衡二叉树5.对称二叉树
    IDEA插件Mybatis Log Plugin的安装及其使用教程
    index.vue?3ae6:112 Uncaught TypeError: this.$message is not a function
    钉钉机器人消息推送composer拓展 laravel-dingbot
    官方解决 maven 上传jar包到中央仓库401
  • 原文地址:https://blog.csdn.net/m0_67401920/article/details/126387030