如果我将相同的键多次传递给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是否像一个引用最后一个值的桶(但在内存中)?


当前回答

你可以在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"),它就变成不被引用的,因此有资格进行垃圾收集。

其他回答

该键的先前值将被删除并替换为新值。

如果你想保留一个键给定的所有值,你可以考虑实现这样的东西:

import org.apache.commons.collections.MultiHashMap;
import java.util.Set;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
public class MultiMapExample {

   public static void main(String[] args) {
      MultiHashMap mp=new MultiHashMap();
      mp.put("a", 10);
      mp.put("a", 11);
      mp.put("a", 12);
      mp.put("b", 13);
      mp.put("c", 14);
      mp.put("e", 15);
      List list = null;

      Set set = mp.entrySet();
      Iterator i = set.iterator();
      while(i.hasNext()) {
         Map.Entry me = (Map.Entry)i.next();
         list=(List)mp.get(me.getKey());

         for(int j=0;j<list.size();j++)
         {
          System.out.println(me.getKey()+": value :"+list.get(j));
         }
      }
   }
}

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

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

更多信息:HashMap文档

它替换对应键的映射中现有的值。如果不存在同名的键,则使用所提供的值创建一个键。 例如:

Map mymap = new HashMap();
mymap.put("1","one");
mymap.put("1","two");

输出 Key = "1", value = " 2 "

所以,之前的值会被覆盖。

你可以在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是线程安全的高性能,因为它使用“锁条带”机制来提高吞吐量。