Possible Duplicate:
Java Hashmap: How to get key from value?
I am looking for a Java data structure (Some sort of map) in which I can perform a lookup on the Keys and the Values. For instance suppose I have a one to one mapping between a set of strings and integers. Call this object mapper. I would like to be able to perform the following:
mapper.getAssociated(value)
: This would return the keymapper.getAssociated(key)
: This would return the valueI think you are looking for google guava BiMap (or) commons BidiMap.
Example:
BidiMap bidiMap = new DualHashBidiMap( );
bidiMap.put( "il", "Illinois" );
bidiMap.put( "az", "Arizona" );
bidiMap.put( "va", "Virginia" );
// Retrieve the key with a value via the inverse map
String vaAbbreviation = bidiMap.inverseBidiMap( ).get( "Virginia" );
// Retrieve the value from the key
String illinoisName = bidiMap.get( "il" );
See this post for BiMap Example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With