wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes 1:1 Map class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "1:1 Map class?" Watch "1:1 Map class?" New topic
Author

1:1 Map class?

Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
Is there a 1:1 map class where you can look up one value based on another?
Say I have a mapping like
1-four
2-three
3-two
4-one
Is there an object where I can look up "one" and get 4, and 2 gets me "three"?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Phil,
Actually it's the Map interface. Probably the most used implementation is the HashMap.
So for your example:

Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Is there an object where I can look up "one" and get 4, and 2 gets me "three"?

Anything is possible in Java:

Eugene.
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
Well, the Map interface has the get function, for returning the value based on the key - but there is no function for returning the key based on the value.
I know I could work around it by doing a cross-mapping - have x->y and y->x, so the map size would be 2x the actual number of pairs - but I'm just wondering if there's a pre-made 1:1 both key/value unique sort of object.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Michael and I engaged in a "Who answers faster?" competition. While I typed my demo, Michael posted his. The solutions are nearly identical, -- so much for diversity of opinion.
Eugene.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

There is an implementation called DoubleOrderedMap from Jakarta Projects under Commons Collections.
http://jakarta.apache.org/commons/collections/
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
And Ryo wins!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

The solutions are nearly identical, -- so much for diversity of opinion.

When you're right, you're right.
Michael Morris
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Look again at Phil's question. Lookups need to go both ways - not just key to value, but also value to key. (Well, the distinction between key and value loses meaning in this case, but you get the idea.) That's where the DoubleOrderedMap comes in. It's not difficult to implement yourself by making a wrapper around two internal Maps (one for each direction of lookup) - but since it's already done for us at Jakarta...
[ March 18, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: 1:1 Map class?
 
Similar Threads
Syntax error on token(s), misplaced construct(s)
synchroniztion of map
TLD Resource path
Gray codes
Compare 2 Maps