Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么使用重复键时会替换Hashmap值,即使它在内部为每个bucket使用链表?_Java_Hashmap - Fatal编程技术网

Java 为什么使用重复键时会替换Hashmap值,即使它在内部为每个bucket使用链表?

Java 为什么使用重复键时会替换Hashmap值,即使它在内部为每个bucket使用链表?,java,hashmap,Java,Hashmap,我正在修改HashMap的概念,只是想检查每个bucket of Entry类的链表实现是如何工作的 public static void main(String[] args) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(1, 1); map.put(1, 2); map.put(1, 3); map.put(2, 1);

我正在修改HashMap的概念,只是想检查每个bucket of Entry类的链表实现是如何工作的

public static void main(String[] args) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(1, 1);
    map.put(1, 2);
    map.put(1, 3);
    map.put(2, 1);
    map.put(2, 2);
    System.out.println(map.values());
}
publicstaticvoidmain(字符串[]args){
HashMap=newHashMap();
图.put(1,1);
图.put(1,2);
地图.put(1,3);
地图.put(2,1);
地图.put(2,2);
System.out.println(map.values());
}
}

上面的代码打印3,2。
它不应该打印1,2,3,1,2吗。

您将值
1,2,3
插入键
1
,将值
1,2
插入键
2
。每次向键中插入值时,都会覆盖该键上以前存在的值(假定存在以前的值)。因此,您的代码在功能上与此相同:

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 3);
map.put(2, 2);
HashMap map=newhashmap();
地图.put(1,3);
地图.put(2,2);

也就是说,只有最新的键值分配才真正“坚持”。谢谢Tom,First link完美地解释了这一点。请让我知道如何删除此问题。