HashMap 重复的key被覆盖

来源:互联网 发布:文泰刻绘端口设置 编辑:程序博客网 时间:2024/04/28 22:31

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值相同的元素。

0 2
原创粉丝点击