• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

1:1 Map class?

 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Ryo wins!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic