如果我将相同的键多次传递给HashMap的put方法,原始值会发生什么变化?如果值重复呢?我没找到任何关于这个的文件。

情况1:键的覆盖值

Map mymap = new HashMap();
mymap.put("1","one");
mymap.put("1","not one");
mymap.put("1","surely not one");
System.out.println(mymap.get("1"));

我们得到的肯定不是1。

案例2:重复值

Map mymap = new HashMap();
mymap.put("1","one");
mymap.put("1","not one");
mymap.put("1","surely not one");
// The following line was added:
mymap.put("1","one");
System.out.println(mymap.get("1"));

我们得到一个。

但是其他的值会怎样呢?我在给一个学生教授基础知识,有人问我这个问题。Map是否像一个引用最后一个值的桶(但在内存中)?


当前回答

将指定值与此映射中的指定键关联。如果映射之前包含键的映射,则替换旧值。

其他回答

是的,这意味着所有有值的1键都被最后一个添加的值覆盖,在这里你添加了“肯定不是一个”,所以它将只显示“肯定不是一个”。

即使您尝试使用循环显示,它也只会显示具有相同键的一个键和值。

根据定义,put命令替换映射中与给定键关联的前一个值(概念上类似于基本类型的数组索引操作)。

映射只是删除了对该值的引用。如果没有其他对象保存对该对象的引用,则该对象符合垃圾收集的条件。此外,Java返回与给定键相关的任何先前值(如果不存在则返回null),因此您可以确定那里有什么,并在必要时维护引用。

更多信息:HashMap文档

你可以在map# put(K, V)的javadoc中找到答案(它实际上会返回一些东西):

public V put(K key, V value) Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.) Parameters: key - key with which the specified value is to be associated. value - value to be associated with the specified key. Returns: previous value associated with specified key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.)

如果你在调用mymap时不分配返回值。Put ("1", "a string"),它就变成不被引用的,因此有资格进行垃圾收集。

顺便说一下,如果你想要一些语义,比如只有当这个键不存在时才放。你可以使用concurrentHashMap与putIfAbsent()函数。 看看这个:

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html将(K, % 20 v)

concurrentHashMap是线程安全的高性能,因为它使用“锁条带”机制来提高吞吐量。

将指定值与此映射中的指定键关联。如果映射之前包含键的映射,则替换旧值。