Skip to content
Permalink
0d82ff1dc4
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
57 lines (44 sloc) 877 Bytes
package kb.howtokb.utils;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author ntandon
*
* @param <K>
* @param <V>
*/
public class BijectiveMap<K, V> extends HashMap<K, V> {
private Map<V, K> v2k;
public BijectiveMap() {
super();
v2k = new HashMap<>();
}
@Override
public V put(K key, V value) {
v2k.put(value, key);
return super.put(key, value);
}
@Override
public V remove(Object key) {
// removing a key, implies removing the corresponding 1:1 value
if (super.containsKey(key))
v2k.remove(super.get(key));
return super.remove(key);
}
public V getValueFromKey(K key) {
return super.get(key);
}
public K getKeyFromValue(V key) {
return v2k.get(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
super.putAll(m);
}
@Override
public void clear() {
super.clear();
v2k.clear();
}
}