Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Mapping Data Structure [duplicate]

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:

  1. mapper.getAssociated(value): This would return the key
  2. mapper.getAssociated(key): This would return the value
like image 555
CodeKingPlusPlus Avatar asked Feb 18 '23 05:02

CodeKingPlusPlus


1 Answers

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

like image 130
kosa Avatar answered Feb 27 '23 15:02

kosa