f



"value" to find a "key"


Is there such a "Map" in java I can easily trace the key by its value,
assuming the  values are also unique ?


John,

0
chunji08 (39)
3/12/2008 10:12:47 PM
comp.lang.java.programmer 52711 articles. 1 followers. Post Follow

6 Replies
667 Views

Similar Articles

[PageSpeed] 52

John wrote:
> 
> Is there such a "Map" in java I can easily trace the key by its value,
> assuming the  values are also unique ?

     Not that I know of.  You could always use two Maps, one
for name-to-phone and the other for phone-to-name.  If you
happen to know *for certain* that names and numbers are never
alike, you could use a single Map and enter each item twice,
once as name-and-phone and once as phone-and-name.

-- 
Eric.Sosman@sun.com
0
Eric
3/12/2008 10:29:45 PM
Thanks Eric,
I think I will try your second suggestion.

John,



On Mar 12, 3:29=A0pm, Eric Sosman <Eric.Sos...@sun.com> wrote:
> John wrote:
>
> > Is there such a "Map" in java I can easily trace the key by its value,
> > assuming the =A0values are also unique ?
>
> =A0 =A0 =A0Not that I know of. =A0You could always use two Maps, one
> for name-to-phone and the other for phone-to-name. =A0If you
> happen to know *for certain* that names and numbers are never
> alike, you could use a single Map and enter each item twice,
> once as name-and-phone and once as phone-and-name.
>
> --
> Eric.Sos...@sun.com

0
John
3/12/2008 10:54:08 PM
On Wed, 12 Mar 2008 15:12:47 -0700 (PDT), John <chunji08@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>Is there such a "Map" in java I can easily trace the key by its value,
>assuming the  values are also unique ?

You create a second map where your values act as keys.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Roedy
3/13/2008 5:10:09 AM
John wrote:
> 
> Is there such a "Map" in java I can easily trace the key by its value,
> assuming the  values are also unique ?

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/bidimap/TreeBidiMap.html
0
Thomas
3/13/2008 6:39:43 AM
On Mar 13, 6:12 am, John <chunj...@gmail.com> wrote:
> Is there such a "Map" in java I can easily trace the key by its value,
> assuming the  values are also unique ?
>
> John,

The map you imaged does not exist in the JDK..
But if the values are unique, you can use two maps to resolve that
problem.
A--B and B--A
0
Nic
3/13/2008 8:54:07 AM
Nic wrote:
> On Mar 13, 6:12 am, John <chunj...@gmail.com> wrote:
>> Is there such a "Map" in java I can easily trace the key by its value,
>> assuming the  values are also unique ?
>>
>> John,
> 
> The map you imaged does not exist in the JDK..
> But if the values are unique, you can use two maps to resolve that
> problem.
> A--B and B--A

You can also have each Map's value be some kind of Pair class that holds the A 
and B values in a single object.  That way you have both items whichever Map 
you used to reach it, and their association is preserved while you're using 
them.  The different Maps analogize to multiple indexes in a database, all 
organizing the same tuples via different lookup keys.

-- 
Lew
0
Lew
3/13/2008 1:36:10 PM
Reply: