Hash Map Question

T

TheVooDooChild

I am using a Hash Map for a list of Key/Value pairs. The map is filled
with String Key's and Integer value's. I am using this for a check
digit routine and at the end of my calculation I need to get the Key
associated with a certain value. Is there any way to get a Key from a
Value? Note: All values are unique.

Maybe I shouldn't be using a HashMap??

Thanks.
 
T

TheVooDooChild

import org.apache.commons.collections.*;

BidiMap map = new DualHashBidiMap();

I get the Error: class BidiMap not found
 
P

Patricia Shanahan

TheVooDooChild said:
I am using a Hash Map for a list of Key/Value pairs. The
map is filled with String Key's and Integer value's. I
am using this for a check digit routine and at the end of
my calculation I need to get the Key associated with a
certain value. Is there any way to get a Key from a
Value? Note: All values are unique.

Maybe I shouldn't be using a HashMap??

Thanks.

One approach is to use a bidirectional map. You can either
pick up an existing implementation, see Cram TeXeD's reply,
or roll your own from two HashMap objects with the keys and
values exchanged.

Alternative, run the simple HashMap until the end. Use its
entrySet method to get all the mappings. How you use the set
depends on how you need to do the lookups, but you could
construct a new HashMap at that point with keys and values
exchanged.

Patricia
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top