将map中的value赋值给list,list改变为什么会引起map也变呢?

Map<Integer, List<Integer>> map1 = new HashMap<>();
        List<Integer> list1 = new ArrayList<Integer>();
        List<Integer> list2 = new ArrayList<Integer>();
        for (int i = 1; i < 6; i++) {
            list1.add(i);
        }
        for (int k = 1; k < 6; k++) {
            map1.put(k, list1);
        }
        System.out.println(map1);
        list2 = map1.get(1);
        list2.remove(0);
        System.out.println(map1);

map除了基本类型是正儿八经的值传递,其余的都是引用地址传递

由于map的value存储的引用地址传递(list),所以当list对象的内部属性发生改变时,map中的value随着改变

原文地址:https://www.cnblogs.com/cjeandailynotes/p/12214328.html