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"?
Is there an object where I can look up "one" and get 4, and 2 gets me "three"?
Anything is possible in Java:
Joined: Feb 15, 2003
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.
Joined: Oct 08, 2001
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.
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 ]