Created
March 26, 2014 11:54
-
-
Save runemart/9781579 to your computer and use it in GitHub Desktop.
Generic HashMap that takes two keys
This file contains hidden or 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
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