"value" to find a "key"

J

John

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


John,
 
E

Eric Sosman

John said:
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.
 
R

Roedy Green

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.
 
N

Nic

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
 
L

Lew

Nic said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top