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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:32:54  来源:igfitidea点击:

Java: convert HashMap values to Set<Integer>

javahashmapset

提问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 entrySetis 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 Setof Integersand not as a Setof Set, then you could do something like this

如果您需要将所有值作为 a SetofIntegers而不是 a Setof Set,那么您可以执行以下操作

Set<Integer> newVariable = new HashSet<Integer>();
for (Set<Integer> set : myWordDict.values()) {
    newVariable.addAll(set);
}

Or if you want them as a Setof Set, you need to do something like this

或者如果你想要它们作为Setof 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

What does entrySetreturns?

什么entrySet回报?

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 myWordDicthash 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

Assuming that you want to add all values from the sets in the map your approach to do it is to use addAllmethod:

假设您想从地图中的集合中添加所有值,您的方法是使用addAll方法:

Set<Integer> newVariable = new HashSet<Integer>();
for (Set<Integer> set : myWordDict.values()) {
    newVariable.addAll(set);
}

回答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());