Java:如何将集保存为HashMap中的值?
我想用下面的数据结构来存储一个具有多个IDs int的单词,但我不知道如何将键值对放入下面的变量myWord中Java:如何将集保存为HashMap中的值?,java,hashmap,set,Java,Hashmap,Set,我想用下面的数据结构来存储一个具有多个IDs int的单词,但我不知道如何将键值对放入下面的变量myWord中 Map<String, Set<Integer>> myWord = new HashMap<String, Set<Integer>>(); myWord.putword1,如何在此处将ID添加到集合中 谢谢你也可以考虑使用A,它更有力地表示你要建立的数据结构。 Map<String, Set<Integer>>
Map<String, Set<Integer>> myWord = new HashMap<String, Set<Integer>>();
myWord.putword1,如何在此处将ID添加到集合中
谢谢
你也可以考虑使用A,它更有力地表示你要建立的数据结构。Map<String, Set<Integer>> myWord = new HashMap<String, Set<Integer>>();
Set<Integer> mySet = new HashSet<Integer>();
myWord.put("word1", mySet);
Multimap<String, Integer> myWord = HashMultimap.create();
myWord.put("word1", 2);
Set<Integer> mySet = new HashSet<Integer>(); // create a set of IDs
mySet.add(1); // add Id to set
mySet.add(2); // add Id to set
myWord.put("word1", mySet); // finally put set in your map
Set mySet = new HashSet<Integer>();
mySet.add(3);
Map<String, Set<Integer>> myWord = new HashMap<String, Set<Integer>>();
myWord.put("word1", mySet);
Multimap<String, Integer> myWord = HashMultimap.create();
myWord.put("word1", 2);
Set<Integer> mySet = new HashSet<Integer>(); // create a set of ids for each word
mySet.add(1); // add Id to the set
mySet.add(2); // add Id to the set
myWord.put("word1", mySet); // for every word put the corresponding set in the map
Map<Integer, Set<Integer>> adjacent = new HashMap<Integer,Set<Integer>>(n);
for (int i = 0; i < m; i++) {
int nodeA = s.nextInt();
int nodeB = s.nextInt();
if (!adjacent.containsKey(nodeA)) { **checking if the nodes contains A**
adjacent.put(nodeA, new HashSet<Integer>()); **If node A is not present it is being added**
}
adjacent.get(nodeA).add(nodeB); **At the end node B is added to node A's list**
}