我看过很多示例,但不能完全掌握。
我需要创建一种方法,将新值插入我的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<>();
}
}
这是一个多映射。
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);
}
}