Essence of BiMap in Google collections
Posted
by
littleEinstein
on Stack Overflow
See other posts from Stack Overflow
or by littleEinstein
Published on 2012-05-31T16:36:56Z
Indexed on
2012/05/31
16:40 UTC
Read the original article
Hit count: 348
I am still quite puzzled at the BiMap in Google collections/Guava. It was claimed that The two bimaps are backed by the same data; any changes to one will appear in the other.
I browsed through the source code, and I found the use of delegate in ForwardingMap
. But in any actually subclass of StandardBiMap
, I do see the data are put into both the forward and reverse map. So what is the essence, and why it claims to have saved space by keeping only one copy of the data? Is it just the actual objects are one set, but two distinct sets of references to these objects are still needed, one set maintained in forward map, and the other in reverse map?
private V putInBothMaps(K key, V value, boolean force) {
boolean containedKey = containsKey(key);
if (containedKey && Objects.equal(value, get(key))) {
return value;
}
if (force) {
inverse().remove(value);
} else if (containsValue(value)) {
throw new IllegalArgumentException(
"value already present: " + value);
}
V oldValue = super.put(key, value);
updateInverseMap(key, containedKey, oldValue, value);
return oldValue;
}
© Stack Overflow or respective owner