Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have a key-key map (as opposed to key-value) in Java?

Tags:

java

map

What if I need to quickly search not only by the key but also by value. In other words, is there a construction like key-key as opposed to key-value?

like image 829
user1081596 Avatar asked Jan 16 '12 15:01

user1081596


2 Answers

Sounds like you want a bimap - I'd use the implementations in Guava if I were you; there's a BiMap interface, and various implementations such as HashBiMap and ImmutableBiMap.

Note that you generally view a BiMap from one "side" (K1 to K2), and just call inverse() to get the opposite view of things (K2 to K1).

like image 163
Jon Skeet Avatar answered Oct 26 '22 22:10

Jon Skeet


Several libraries have something like that. For example, Google Guava has a BiMap (bidirectional map). Unfortunately there's no bidirectional map in the standard Java library.

like image 21
Jesper Avatar answered Oct 26 '22 22:10

Jesper