File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Maps" Watch "Maps" New topic


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

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

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?
I agree. Here's the link:
subject: Maps
jQuery in Action, 3rd edition