Java key - key card

I need some kind of card, available in two directions, so with a key-key instead of a key value. Does it exist in Java? If not, what is the best way to create it?

So an example:

mySpecialHashMap.put("key1", "key2");

mySpecialMap.getL2R("key1") returns "key2";
mySpecialMap.getR2L("key2") returns "key1";
+5
source share
4 answers

So you need a bi-directional map. You can use Apache Commons Collectionments BidiMap or Google Collections BiMap for this.

+22
source

You can look at BiMap from Guava (formerly known as Google Collections).

, HashBiMap "mySpecialHashMap":

BiMap<String, String> myBiMap = HashBiMap.create();
myBiMap.put("key1", "key2");

myBiMap.get("key1"); // returns "key2"
myBiMap.inverse().get("key2"); // returns "key1"
+3

Yes, there is BiMap from Google Collections.

+2
source

Or see. For reversible transfers fooobar.com/questions/638951 / ... .

+1
source

All Articles