java 循环遍历 hashmap 将相同键的值分组为 <key, list<values>> 对

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15115991/
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-10-31 18:31:43  来源:igfitidea点击:

Looping through hashmap to group the values of same key into a <key, list<values>> pair

javahashmap

提问by user974047

I've been having a hard time trying to think of a way create a HashMap that groups values (into a list) that has the same key. This is what I mean:

我一直很难想出一种方法来创建一个 HashMap 将具有相同键的值分组(到列表中)。这就是我的意思:

Say I have the following keys and values:

假设我有以下键和值:

Value     Key  *Sorry I got the columns swapped
1         10 
1         11 
1         12 
2         20 
3         30 
3         31 

I would like to put these values into a

我想将这些值放入

Hashmap <Integer, List<Integer>>

So that it will group the values into the List Integer that has the same key, something like this:

以便它将值分组到具有相同键的 List Integer 中,如下所示:

(1, {10, 11, 12}),(2, {20}), (3, {30,31})

(1, {10, 11, 12}),(2, {20}), (3, {30,31})

Right now the key and the value are stored in a

现在键和值存储在一个

Hashmap <Integer, Integer>

And I'm lost at how to loop through this Hashmap to create the new Hashmap with the key: List of Values pair. Does anyone have a good approach to this topic?

我不知道如何循环遍历此 Hashmap 以使用键创建新的 Hashmap:List of Values 对。有没有人对这个话题有好的方法?

回答by Fortega

Suppose you create a HashMap<Integer, List<Integer>>, and you want to add a key-value pair to it the way you asked, you can use the following method:

假设您创建了一个HashMap<Integer, List<Integer>>,并且您想按照您的要求向其中添加一个键值对,您可以使用以下方法:

public void addToMap(HashMap<Integer, List<Integer>> map, Integer key, Integer value){
  if(!map.containsKey(key)){
    map.put(key, new ArrayList<>());
  }
  map.get(key).add(value);
}

Using this method with your example data:

将此方法与您的示例数据一起使用:

HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
addToMap(map, 1, 10); 
addToMap(map, 1, 11);
addToMap(map, 2, 20);
addToMap(map, 3, 30);
addToMap(map, 3, 31);

回答by zagyi

Instead of a plain Mapuse Google Guava's Multimap.

而不是简单地Map使用谷歌番石榴的Multimap.

A Multimapis a

一个Multimap

...collection that maps keys to values, similar to Map, but in which each key may be associated with multiple values.

...将键映射到值的集合,类似于 Map,但其中每个键可能与多个值相关联。

This concept has of course been implemented in other libraries as well, Guava is just my personal preference.

这个概念当然也已经在其他库中实现了,Guava 只是我个人的喜好。

回答by Rapha?l

HashMap will only store 1 value for each Integer. So iterating over it should only gives you the following values:

HashMap 只会为每个整数存储 1 个值。所以迭代它应该只给你以下值:

Key      Value 
1         12 
2         20 
3         31 

To iterate over the content of a Map you can use the entrySet()method:

要遍历 Map 的内容,您可以使用entrySet()方法:

for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}

To build a Map of List, I recommand doing this:

要构建列表地图,我建议这样做:

List<Integer> list = map.get(key);
if(list == null) {
    list = new ArrayList<Integer>();
    map.put(key, list);
}
list.add(value);

回答by Hyman

Your actual situation can't work because a HashMap<Integer,Integer>is not able to store two pairs with same key as in 1,10and 1,11.

您的实际情况无法正常工作,因为 aHashMap<Integer,Integer>无法存储与 in1,10和相同键的两对1,11

You can easily develop your own multimap but the best thing to do is using a class already developed for that, Apache Commonsframework has a MultiValueMap<K,V>class already prepared for you.

您可以轻松开发自己的多映射,但最好的做法是使用已经为此开发的类,Apache Commons框架MultiValueMap<K,V>已经为您准备了一个类。