Group a table by column and save them to separate lists
posted 6 months ago
This is what my table in the database looks like.
I'm fetching them from the database and I want to save value corresponding to 1, 2 and 3 in separate lists. I know (maybe!) Map<Key, List<Integer>> is what I need but I don't know how to do it.
Output expected is:
In pseudo (we don't hand out ready made code here at the ranch)
Query for all data from DB
Iterate over each record
Lookup map if entry for key (num) exists
If yes, retrieve value (list) and add db value to list
If no, create key+value entry with existing values.