HashMap 重复的key被覆盖

    xiaoxiao2026-09-29  16

    HashMap相同的key,会被覆盖。 如下测试: public static void main(String[] args) { HashMap < Integer, String> hmap = new HashMap<>();

    hmap.put(1, "abc"); hmap.put(1, "ABC"); hmap.put(1, "xyz");

    System.out.println(“hmap size: ” + hmap.size()); ArrayList list = new ArrayList<>(hmap.values()); System.out.println(“list size: ” + list.size()); for (int i = 0; i < hmap.size(); i++) { System.out.println(list.get(i)); } }

    运行结果如下: hmap size: 1 list size: 1 xyz

    因此,HahMap不能添加key值相同的元素。

    转载请注明原文地址: https://ju.6miu.com/read-1312451.html
    最新回复(0)