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:
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:
(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?
Map?HashMapare unique, I bet most of your information is lost. Do a simplefor (Map.Entry<Integer, Integer> e : yourMap) { out.println(e.getKey() + " " + e.getValue()); }loop to check the content of your current map.