Java 如何将单个值添加到hashmap中的列表中?

Java 如何将单个值添加到hashmap中的列表中?,java,Java,我看过这么多的例子,但不能完全理解这一点 我需要创建一个方法,将新值插入hashmap中已填充的列表中。我一辈子都不知道该怎么做。有人能解释一下它是如何工作的吗 我已经创建了填充贴图等的方法。我只是不知道如何创建一个只为特定键插入值的方法 import java.util.*; public class Singles { // instance variables - replace the example below with your own private Map<

我看过这么多的例子,但不能完全理解这一点

我需要创建一个方法,将新值插入hashmap中已填充的列表中。我一辈子都不知道该怎么做。有人能解释一下它是如何工作的吗

我已经创建了填充贴图等的方法。我只是不知道如何创建一个只为特定键插入值的方法

import java.util.*;


public class Singles
{
   // instance variables - replace the example below with your own
   private Map<String, List<String>> interests;

   /**
    * Constructor for objects of class Singles
    */
   public Singles()
   {
      // initialise instance variables
      super();
      this.interests = new HashMap<>();
   }


}
import java.util.*;
公务舱单打
{
//实例变量-将下面的示例替换为您自己的
私人地图利益;
/**
*单类对象的构造函数
*/
公共单打()
{
//初始化实例变量
超级();
this.interests=new HashMap();
}
}
这是一个多重映射

public class MultiMap {
    private Map<String, List<String>> multiMap = new HashMap<>();

    public void put(String key, String value) {
        List<String> values = (this.multiMap.containsKey(key) ? this.multiMap.get(key) : new ArrayList<>());
        values.add(value);
        this.multiMap.put(key, values);
    }
}
公共类多重映射{
私有映射multiMap=newhashmap();
公共void put(字符串键、字符串值){
列表值=(this.multiMap.containsKey(键)?this.multiMap.get(键):new ArrayList());
增加(价值);
this.multiMap.put(键、值);
}
}

兴趣。获取(“你的钥匙”)。添加(“你的价值”)?@Mena比需要的稍多一些,如果出于某种原因,map不包含给定键的值,则会导致NPE。当然可能是@Amongalen的重复项。取决于用户是否提前知道哪些密钥。我只是想说这很简单。