Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maps

 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • 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?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
quhao baggy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic