还有一个关于 Java 中的 HashMap<> 的问题:
我有以下内容
Map<String, Set<Integer>> myWordDict = new HashMap<String, Set<Integer>>();
将数据存入变量后myWordDict
,我想遍历 HashMapValues,并将每个值添加到一个新的 Set 变量?
当我尝试做 Set<Integer> newVariable = myWordDict.entrySet()
,似乎数据类型不兼容。
所以我的问题本质上是:
how to convert HashMap values or entrySet() to Set ?
谢谢
最佳答案
尝试:
Set<Integer> newVariable = mywordDict.keySet();
或
Set<Integer> newVariable = new HashSet<Integer>(myWordDict.values());
关于Java:将 HashMap 值转换为 Set<Integer>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19922181/