You can do a simple implementation as follows. Please note that data is not copied in this implementation. Only links! I added an implementation to add and receive. delete and another necessary method left as an exercise :)
public class TwoWayHashmap<K extends Object, V extends Object> { private Map<K,V> forward = new Hashtable<K, V>(); private Map<V,K> backward = new Hashtable<V, K>(); public synchronized void add(K key, V value) { forward.put(key, value); backward.put(value, key); } public synchronized V getForward(K key) { return forward.get(key); } public synchronized K getBackward(V key) { return backward.get(key); } }
And, of course, his responsibility to apply even βvaluesβ is unique. Usage example:
TwoWayHashmap twmap = new TwoWayHashmap<String, String>(); twmap.add("aaa", "bbb"); twmap.add("xxx", "yyy"); System.out.println(twmap.getForward("xxx")); System.out.println(twmap.getBackward("bbb"));
Gopi Aug 07 '10 at 11:20 2010-08-07 11:20
source share