Java:将 HashMap 值转换为 Set<Integer>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19922181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: convert HashMap values to Set<Integer>
提问by TonyGW
One more question about HashMap<> in Java:
关于 Java 中 HashMap<> 的另一个问题:
I have the following
我有以下
Map<String, Set<Integer>> myWordDict = new HashMap<String, Set<Integer>>();
After storing data into the variable myWordDict
, I want to iterate through the HashMapValues, and add each value to a new Set variable?
将数据存储到变量后myWordDict
,我想遍历 HashMapValues,并将每个值添加到新的 Set 变量中?
When I try to do Set<Integer> newVariable = myWordDict.entrySet()
, it seems the data type is incompatible.
当我尝试这样做时Set<Integer> newVariable = myWordDict.entrySet()
,数据类型似乎不兼容。
So my question is essentially:
所以我的问题本质上是:
how to convert HashMap values or entrySet() to Set ?
如何将 HashMap 值或 entrySet() 转换为 Set ?
Thanks
谢谢
采纳答案by Masudul
Your declaration should be like below. It will convert your map values to Collection
您的声明应如下所示。它会将您的地图值转换为Collection
Collection<Set<Integer>> newVariable = myWordDict.values();
回答by ApproachingDarknessFish
use myWordDict.values()
, not myWordDict.entrySet()
. values is a set of the map's values
, whereas entrySet
is a set of its mappings (it is a set of java.util.map.Entry objects, each of which describes a key and a value).
使用myWordDict.values()
,不是myWordDict.entrySet()
。values 是一组映射values
,而entrySet
是一组映射(它是一组 java.util.map.Entry 对象,每个对象描述一个键和一个值)。
回答by SudoRahul
If you need all the values as a Set
of Integers
and not as a Set
of Set
, then you could do something like this
如果您需要将所有值作为 a Set
ofIntegers
而不是 a Set
of Set
,那么您可以执行以下操作
Set<Integer> newVariable = new HashSet<Integer>();
for (Set<Integer> set : myWordDict.values()) {
newVariable.addAll(set);
}
Or if you want them as a Set
of Set
, you need to do something like this
或者如果你想要它们作为Set
of Set
,你需要做这样的事情
Set<Set<Integer>> newVariable = new HashSet<Set<Integer>>();
newVariable.addAll(myWordDict.values());
回答by Rakesh KR
Map<Integer,String> map1 = new HashMap<Integer,String>();
map1.put(1, "Rakesh");
map1.put(2, "Amal");
map1.put(3, "Nithish");
Set<Entry<Integer,String>> set1 = map1.entrySet();
回答by Aniket Kulkarni
public Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
返回此映射中包含的映射的 Set 视图。该集合由地图支持,因此对地图的更改会反映在该集合中,反之亦然。
If you want iterate over the myWordDict
hash map then just
如果您想遍历myWordDict
哈希映射,那么只需
for(Map.Entry<String, Set<Integer>> entry : myWordDict.entrySet())
{
Set<Integer> newVariable = entry.getValue();
}
Related links
相关链接
回答by Kamlesh Arya
Try:
尝试:
Set<Integer> newVariable = mywordDict.keySet();
or
或者
Set<Integer> newVariable = new HashSet<Integer>(myWordDict.values());
回答by aga
回答by TonyGW
So, I came up with an iterator to iterate through the value Set
所以,我想出了一个迭代器来迭代值 Set
I have this data structure
我有这个数据结构
Map<String, Set<Integer>> myWordDict = new HashMap<String, Set<Integer>>();
I created
我创建
Iterator mapIterator = myWordDict.entrySet().iterator();
Then just use a while() loop to add individual value to a new Set variable. I am not sure this is the most efficient way, but it works for my needs.`
然后只需使用 while() 循环将单个值添加到新的 Set 变量中。我不确定这是最有效的方法,但它适合我的需要。`
回答by freeն
flatMap will help you:
flatMap 将帮助您:
myWordDict.values().stream().flatMap(Set::stream).collect(Collectors.toSet());