You may use any class that implements the "BidiMap" interface in the common collection from Apache (http://commons.apache.org/collections/). This is more efficient because the bidirectional map is constructed when you filled and there is no need to create a new map, which may not be practical when the map is big.
BidiMap aMap = new DualHashBidiMap();
aMap.put("B", "A");
aMap.put("A", "B");
aMap.put("C", "D");
aMap.put("X", "D");
MapIterator it = aMap.mapIterator();
System.out.println("Before Inverse");
while (it.hasNext()) {
key = it.next();
value = it.getValue();
out.println(key + " -> " + value);
}
aMap = aMap.inverseBidiMap();
System.out.println("After Inverse");
it = aMap.mapIterator();
while (it.hasNext()) {
key = it.next();
value = it.getValue();
out.println(key + " -> " + value);
}
Before Inverse
A -> B
B -> A
X -> D
After Inverse
D -> X
A -> B
B -> A