Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking a data structure that is a Map but in which keys can be values, values can be keys

A Map maps from keys to values and provides quick access based on the knowledge of the key.

Does there exist a data structure like Maps which supports both key to value and value to key access? Sometimes I may want to derefence the Map via the value, while other times by the key.

like image 377
ashitaka Avatar asked Jan 29 '26 00:01

ashitaka


2 Answers

Not in the JDK, but you can find a good BiMap implementation in the Google Collections : http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/BiMap.html

like image 156
Guillaume Avatar answered Jan 31 '26 15:01

Guillaume


Apache Commons Collections provides a BidiMap which serves this purpose.

like image 36
pugmarx Avatar answered Jan 31 '26 15:01

pugmarx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!