我一直很难想出一种方法来创建一个 HashMap,它将具有相同键的值(到一个列表中)分组。这就是我的意思:
假设我有以下键和值:
Value Key *Sorry I got the columns swapped
1 10
1 11
1 12
2 20
3 30
3 31
我想将这些值放入
Hashmap <Integer, List<Integer>>
这样它将值分组到具有相同键的 List Integer 中,如下所示:
(1, {10, 11, 12}),(2, {20}), (3, {30,31})
现在键和值存储在一个
Hashmap <Integer, Integer>
而且我不知道如何遍历这个 Hashmap 以使用键创建新的 Hashmap:值对列表。有没有人有这个话题的好方法?