• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Group a table by column and save them to separate lists

 
Eddie Cross
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what my table in the database looks like.

NumValue
110
120
230
215
325
335


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:



Code is appreciated. Thanks in advance.
 
Maneesh Godbole
Saloon Keeper
Posts: 11312
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Repeat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic