创建一个Hashmap,其键为字符串值为HashSets

时间:2013-03-31 19:45:25

标签: java hashmap hashset

尝试使用字符串键和HashSets值创建哈希映射。我希望哈希集都是整数

Set<String> numbersSet = new HashSet<Integer>();

// won't work:
HashMap<String, numberSet> database = new HashMap<String, numberSet>();//error - "( or [ expected"

//Also won't work. If it did, how can even I add to the set inside this hashMap?
HashMap<String, HashSet<Integer>> database = new HashMap<String, HashSet<Integer>>(); //error-incompatible types

1 个答案:

答案 0 :(得分:1)

HashMap<String, HashSet<Integer>> database = new HashMap<String, HashSet<Integer>>();

适合我。

顺便说一句,如果你使用的是JDK 1.7,你可以使用:

HashMap<String, HashSet<Integer>> mymap = new HashMap<>();