Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

From Array of Objects to HashMap

 
Patrice Ricard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone
I have an Array of Objects of type Result. One of the property of this Object is dictionary.
I want to create a HashMap which keys are the different dictionaries and values are Array of Result that have the same dictionary property.

hope to be clear.

thanks
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sample code could look like,

 
Patrice Ricard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fast reply but that doesn't work
You put a Result Object in the values and not an Array of Result.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a MultiHashMap from Apache Commons. Or something similar.

The idea:
1) Map will be Map<Disctionary, List<Results>>.
2) When you get a Dictionary use map.get(dictionary)
3a) If the result is null, make a new List<Results>
3b) Add the current Results object to the List
3c) Put the List back in the Map keyed to the Dictionary

The MultiMap does this for you.
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, should have read the question clearly. Modified code may look like,



And convert the List<Result> to Result[] whenever you want to use the array object. Or if you want to do then itself, loop through the Map, read each list and convert to array object.

As Steve mentioned, MultiHashMap does the same thing.
[ August 19, 2008: Message edited by: Santhosh Kumar ]
 
Patrice Ricard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both of you and for your quickness.

I was stuck with Array in my mind and didn't think about List.toArray().
[ August 19, 2008: Message edited by: Patrice Ricard ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic