Skip to content

Instantly share code, notes, and snippets.

@mzcydev
Created April 21, 2025 15:12
Show Gist options
  • Select an option

  • Save mzcydev/6394520943407c2769d2696a1f97c869 to your computer and use it in GitHub Desktop.

Select an option

Save mzcydev/6394520943407c2769d2696a1f97c869 to your computer and use it in GitHub Desktop.
Simple map containing a value paired to two keys. Could be useful.
package dev.pvpduels.util;
import java.util.HashMap;
import java.util.Map;
public class TwoKeyedMap<K1, K2, V> {
private final Map<K1, Map<K2, V>> map = new HashMap<>();
public void put(K1 key1, K2 key2, V value) {
map.computeIfAbsent(key1, k -> new HashMap<>()).put(key2, value);
}
public V get(K1 key1, K2 key2) {
Map<K2, V> innerMap = map.get(key1);
return innerMap != null ? innerMap.get(key2) : null;
}
public boolean containsKeys(K1 key1, K2 key2) {
return map.containsKey(key1) && map.get(key1).containsKey(key2);
}
public V remove(K1 key1, K2 key2) {
Map<K2, V> innerMap = map.get(key1);
if (innerMap != null) {
V value = innerMap.remove(key2);
if (innerMap.isEmpty()) {
map.remove(key1);
}
return value;
}
return null;
}
public void clear() {
map.clear();
}
public boolean isEmpty() {
return map.isEmpty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment