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?
What you are looking for is essentially implemented by Guava's
BiMap.You can use guava's BiMap like this -
Link:
- Guide to guava BiMap
- BiMap java doc
Alternatively, you can use apache common
BiDiMaplike this: