As we know, there is the concept of BiMap
and MultiMap
but is there a MultiBiMap
? so what do I mean by this.
In MultiMap
you have one-to-many relationship between K and V, a single key can be associated to multiple value, hence the name.
In BiMap
you have K,V pair which is bi-directional mean you can get V,K relationship as well. Like having a two regular maps but synchronized. I need a bi directional multi map where you combine these two concepts.
Do we have a MultiBiMap?
public class ManyToMany<K, V> {
private final Map<K, Set<V>> values = new HashMap<>();
private final Map<V, Set<K>> keys = new HashMap<>();
public Set<V> getValues(K key) {
return values.get(key);
}
public Set<K> getKeys(V value) {
return keys.get(value);
}
public boolean put(K key, V value) {
return values.computeIfAbsent(key, k -> new HashSet<>()).add(value)
&& keys.computeIfAbsent(value, v -> new HashSet<>()).add(key);
}
}
I wrote something similar a little while a go but I do not like the fact that I had two lists but this should work too. –
Slocum
yes I agree but it would be nice to have it implemented and abstracted for you. Instead of writing a it yourself. :-) –
Slocum
© 2022 - 2024 — McMap. All rights reserved.
"a"->[1, 2, 3]
,getKeyForValue(2) == "a"
? – Hitlerism"a"->[1, 2]
and"b"->[2, 3]
, then what key is associated with the value2
? – Hitlerism"a"->[1, 2]
and"b"->[2, 3]
, then you wantgetInverse(2) == ["a", "b"]
? And do you need it to be mutable? – Hitlerism