The docs for com.google.common.collect.HashBiMap don't state whether it is thread-safe or not. I guess that means it isn't, but thought I'd ask in case I'm wrong.
HashBiMap uses multiple custom hashtables internally, which are not thread safe. You should synchronize around accesses to HashBiMap
with:
Maps.synchronizedBiMap(yourHashBiMap);
Look at the source code, it doesn't seem synchronized to me .
private V More ...putInBothMaps(@Nullable K key, @Nullable V value, boolean force) {
boolean containedKey = containsKey(key);
if (containedKey && Objects.equal(value, get(key))) {
return value;
}
if (force) {
inverse().remove(value);
} else {
checkArgument(!containsValue(value), "value already present: %s", value);
}
V oldValue = delegate.put(key, value);
updateInverseMap(key, containedKey, oldValue, value);
return oldValue;
}
Here delegate
and inverseMap
are the two Map
s it uses internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With