• 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

Maps

 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I use a map if I want to have multiple sets of keys for each object?
I want to have a map of nodes which I can access by ID, Name, and maybe something else.
Is the only soln to have 3 maps, 1 for each kind of key?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you want to simply use one map, and make three entries for each value object, where each entry uses one of the different keys.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maps may be a bad choice if any index key can get multiple items. Can you have Name=Dave with ID=1 and another Name=Dave with ID=2? Map will only store one object with key Dave. If you have a db background, all keys must be primary key candidates.
A solution might be to store a collection at each non-unique key. When you get Dave from the map, you get an ArrayList full of Daves.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you try the hashmap? i think you can use the key to get the elements in your map
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each key brings a unique item But different objects who may access the collection may have different information and thus I want to be able to use different/multiple keys to find objects in my collection
For ex Object A may only know of my node IDs, but object B may only know about node Name. Bot will uniquely map to a particular node. I would like to be able to have one central collection if possible, but allow both objects A and B to be able to find nodes in this collection using the key they know. Essentially I'd like this fcn
collection.put(key1,key2,key3, object);
Can this be done with a single collection, or do I need to find another method?
reply
    Bookmark Topic Watch Topic
  • New Topic