java - 制作一个 Hashmap,其键是字符串,值是 HashSets

标签 java hashmap hashset

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

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

最佳答案

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

对我有用。

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

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

关于java - 制作一个 Hashmap,其键是字符串,值是 HashSets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15733733/

相关文章:

java - 将 long 列表转换为 int java 8 列表

java - 过滤和减少之间的区别?

java - HashMap 和数组列表

powershell - 传入空集会引发错误

Java 集合 : What collection to use is this very specific case?

c# - 删除自定义 HashSet

java - 我怎样才能得到句子中的确切单词

java - RedisTemplate 导致 Fortify 动态代码评估中的不安全反序列化

java - 如何创建包含所有java类常量的HashMap或Arraylist

java - 快速搜索不断增长的大型平面文件的最佳方法是什么?