File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

1:1 Map class?

 
Phil Chuang
Ranch Hand
Posts: 251
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil,
Actually it's the Map interface. Probably the most used implementation is the HashMap.
So for your example:

Michael Morris
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 251
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an implementation called DoubleOrderedMap from Jakarta Projects under Commons Collections.
http://jakarta.apache.org/commons/collections/
 
Phil Chuang
Ranch Hand
Posts: 251
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Ryo wins!
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

When you're right, you're right.
Michael Morris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic