2

Is there a data structure in Java (java util, guava...) that allows me to store "key value" pairs that can also be interpreted as value-key?

Example:

Datastructure d = new Datastructure();

d.add(1, "foo");
d.add(21 "bar");
d.add(33 "hello");
d.add(55 "world");

A function like d.get1(1) should return foo. A function like d.get2("foo") should return 1.

A function like d.get1(33) should return hello. A function like d.get2("hello") should return 33.

...

Is there something that works like this?

Razib
  • 10,965
  • 11
  • 53
  • 80
hardfork
  • 2,470
  • 1
  • 23
  • 43

4 Answers4

2

In the Apache commons collections library is the BidiMap interface and some implementations thereof. The interface has a method to get the inverse map, i.e. a map where role of key and value are swapped.

If you don't want to use this class you could easily implement your own class using two maps internally.

Jannik
  • 1,583
  • 1
  • 14
  • 22
1

There is not such a class in Java, but you can do what you want with this:

Map<Object, Object> map = new HashMap<>();

map.put(1, "foo");
map.put(21, "bar");
map.put(33, "hello");
map.put(55, "world");
map.put("foo", 1);
map.put("bar", 21);
map.put("hello", 33);
map.put("world", 55);

System.out.println(map.get(1)); // foo
System.out.println(map.get("foo")); // 1

Looks and is weird, but works as you want.

Or you can create your own class that works as you need, maybe using one map as I showed, or two maps like: Map<Integer, String> map1... Map<String, Integer> map2...

Hope it helps.

Jeremy Then
  • 525
  • 2
  • 12
  • 1
    thanks. That would work for my simple example. But a problem could occur if it's a Map for example with the entries (3,1) and (2,3) for example. No problem with map.put(1,3), but map.put(3,2) would overwrite the (3,1) entry. – hardfork May 13 '19 at 21:28
  • Use 2 different maps in that case. – Jeremy Then May 13 '19 at 22:09
1

What you are looking for is essentially implemented by Guava's BiMap.
You can use guava's BiMap like this -

BiMap<Integer, String> biMap = HashBiMap.create();
biMap.put(1, "foo");
biMap.put(21, "bar");

System.out.println(biMap.get("1"))); //foo
System.out.println(biMap.inverse().get("bar"))); //21

Link:
- Guide to guava BiMap
- BiMap java doc

Alternatively, you can use apache common BiDiMap like this:

BidiMap<String, String> map = new DualHashBidiMap<>();
map.put(1, "foo");
map.put(21, "bar");
System.out.println(map.get(1)); //1

//reversing the mapping 
BidiMap<String, String> reversedMap = map.inverseBidiMap();
System.out.println(reversedMap.get("foo")); //1
Razib
  • 10,965
  • 11
  • 53
  • 80
1

Yes, Guava has the BiMap interface, with a variety of implementations, including HashBiMap and ImmutableBiMap, which provide an inverse() view to do a reverse lookup. Note also that they are implemented more efficiently than simply storing a pair of maps, one in each direction -- though at some level, it's impossible to build a significantly better implementation.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413