Permalink
Cannot retrieve contributors at this time
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?
HowToKB/src/kb/howtokb/utils/BijectiveMap.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
57 lines (44 sloc)
877 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |