119

I have a simple integer-to-string mapping in Java, but I need to be able to easily retrieve string from integer, and also integer from string. I've tried Map, but it can retrieve only string from integer, it's one way:

private static final Map<Integer, String> myMap = new HashMap<Integer, String>();
// This works one way:
String myString = myMap.get(myInteger);

// I would need something like:
Integer myInteger = myMap.getKey(myString);

Is there a right way to do it to have it both directions?

Another problem is that I only have a few constant values that don't change (1->"low", 2->"mid", 3->"high", so it wouldn't be worth to go for a complicated solution.

husayt
  • 14,553
  • 8
  • 53
  • 81
Danijel
  • 8,198
  • 18
  • 69
  • 133
  • https://stackoverflow.com/questions/72487163/bidirectional-mapping-in-java-witch-rejects-entries-of-key-or-value-already-exis demonstrates a solution based on [this][1] tutorial. [1]: https://www.tutorialspoint.com/commons_collections/commons_collections_bidimap.htm – Simeon Jun 03 '22 at 09:23

6 Answers6

98

You can use the Google Collections API for that, recently renamed to Guava, specifically a BiMap

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

epoch
  • 16,396
  • 4
  • 43
  • 71
  • See http://stackoverflow.com/a/39329395/5466401 – Sibin John Mattappallil Sep 05 '16 at 11:15
  • 3
    Google probably needs BiMap for specific use cases. But, why does Java not provide BiMap ? Its nice to have options in data structures & algorithms instead of having to get them from libraries or coding from scratch. – MasterJoe Jul 11 '20 at 19:31
54

Creating a Guava BiMap and getting its inverted values is trivial.

A simple example:

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

public class BiMapTest {

  public static void main(String[] args) {

    BiMap<String, String> biMap = HashBiMap.create();

    biMap.put("k1", "v1");
    biMap.put("k2", "v2");

    System.out.println("k1 = " + biMap.get("k1"));
    System.out.println("v2 = " + biMap.inverse().get("v2"));
  }
}
Ali Faradjpour
  • 302
  • 6
  • 15
Michal Zmuda
  • 5,381
  • 3
  • 43
  • 39
31

There is no bidirectional map in the Java Standard API. Either you can maintain two maps yourself or use the BidiMap from Apache Collections.

gawi
  • 2,843
  • 4
  • 29
  • 44
Mathias Schwarz
  • 7,099
  • 23
  • 28
16

You could insert both the key,value pair and its inverse into your map structure, but would have to convert the Integer to a string:

map.put("theKey", "theValue");
map.put("theValue", "theKey");

Using map.get("theValue") will then return "theKey".

It's a quick and dirty way that I've made constant maps, which will only work for a select few datasets:

  • Contains only 1 to 1 pairs
  • Set of values is disjoint from the set of keys (1->2, 2->3 breaks it)

If you want to keep <Integer, String> you could maintain a second <String, Integer> map to "put" the value -> key pairs.

Chicowitz
  • 5,759
  • 5
  • 32
  • 38
11

Apache commons collections has a BidiMap

gawi
  • 2,843
  • 4
  • 29
  • 44
hage
  • 5,966
  • 3
  • 32
  • 42
5

Use Google's BiMap

It is more convenient.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
BOSS
  • 2,931
  • 8
  • 28
  • 53