Two Laptop Bag*
The moose likes Beginning Java and the fly likes Maps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Maps" Watch "Maps" New topic
Author

Maps

Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
quhao baggy
Greenhorn

Joined: Dec 10, 2003
Posts: 5
did you try the hashmap? i think you can use the key to get the elements in your map
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
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?
 
Don't get me started about those stupid light bulbs.
 
subject: Maps
 
Similar Threads
Creating a TreeMap form Enumerated Zip Entries
Design decision - data objects
BlueJ Error Message Please Help
More than a sorting with dupplicate elimination
Maps of current Location