• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

From Array of Objects to HashMap

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sample code could look like,

 
Patrice Ricard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic