Skip to content

Instantly share code, notes, and snippets.

@runemart
Created March 26, 2014 11:54
Show Gist options
  • Save runemart/9781579 to your computer and use it in GitHub Desktop.
Save runemart/9781579 to your computer and use it in GitHub Desktop.
Generic HashMap that takes two keys
public class TwoKeyHashMap<K1, K2, V> extends HashMap<K1, HashMap<K2, V>> {
public void put(K1 key_1, K2 key_2, V value) {
if (containsKey(key_1))
get(key_1).put(key_2, value);
else {
HashMap<K2, V> inner = new HashMap<>();
inner.put(key_2, value);
put(key_1, inner);
}
}
public V get(K1 key_1, K2 key_2) {
if (contains(key_1, key_2))
return get(key_1).get(key_2);
return null;
}
public boolean contains(K1 key_1, K2 key_2) {
HashMap<K2, V> inner = get(key_1);
if (inner == null)
return false;
return inner.containsKey(key_2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment