Should HashMap::computeIfAbsent be considered a "structural modification" for an existing key?

Michael Rasmussen Michael.Rasmussen at roguewave.com
Mon Nov 19 16:01:16 UTC 2018


Hi

As the topic asks, should calling computeIfAbsent on a HashMap with an existing key be a structural modification?

The documentation for HashMap has the following note regarding concurrency:
A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.

Especially the last part there makes me argue that computeIfAbsent with an existing key should NOT be a structural modification -- especially considering the equivalent call to HashMap::put is not.

Yet, it currently can be, as computeIfAbsent might resize the underlying table _before_ checking if the key is present.
(computeIfAbsent has if (size > threshold) resize() in the beginning, vs putVal having it at the end, after the key has been added).

So if HashMap::computeIfAbsent with an existing key should not be a structural modification, the current implementation is bugged.

Kind regards
Michael Rasmussen


More information about the core-libs-dev mailing list