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

What if I need to quickly search for not only the key, but also the value. In other words, is there such a design as a key-key, and not a key-value?

+8
java map
source share
3 answers

It sounds like you want bimap - I would use implementations in Guava if I were you; There is a BiMap interface and various implementations, such as HashBiMap and ImmutableBiMap .

Note that you usually look at BiMap from one β€œside” (from K1 to K2) and simply call inverse() to get the opposite view of things (from K2 to K1).

+9
source share

Several libraries have something similar. For example, Google Guava has a BiMap (bi-directional map). Unfortunately, there is no bi-directional map in the standard Java library.

+5
source share

To clarify, you will have some kind of card with the following key pairs: value:

 Map<String, String> map = new HashMap<String, String>(); map.put("foo", "Freddy"); map.put("bar", "Bobby"); 

Then would you like to make map.get ("foo") and get Freddy, or do map.get ("freddy") and get foo?

If so, check this post .

0
source share

All Articles