【问题标题】:Make A Hashmap with its keys being Strings values being HashSets制作一个 Hashmap,其键是字符串,值是 HashSet
【发布时间】:2013-03-31 19:45:24
【问题描述】:

试图用字符串的键和 HashSet 的值创建一个 Hash Map。我希望哈希集都是整数

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

【问题讨论】:

  • Map&lt;String, Set&lt;Integer&gt;&gt; mapOfSets = new HashMap&lt;String, Set&lt;Integer&gt;&gt;(); Set&lt;Integer&gt; numbersSet = new HashSet&lt;Integer&gt;(); mapOfSets.put("numbersSet", numbersSet);,为我工作。
  • Luiggi,你应该把它作为答案。

标签: java hashmap hashset


【解决方案1】:
HashMap<String, HashSet<Integer>> database = new HashMap<String, HashSet<Integer>>();

为我工作。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 2013-11-23
    相关资源
    最近更新 更多