简体   繁体   中英

Check if computeIfAbsent of ConcurrentHashMap changed something

How do you know that the function computeIfAbsent of a ConcurrentHashMap had to call the given generator method ("mappingFunction")?
From the Javadoc I believe it returns the new value, if one was generated and the old value if one existed. I could set an external flag from a lambda generator function but that would be awkward...

You could use compute instead:

map.compute(key, (k, v) -> v == null ? /*absent*/ this::getValue : /*present*/ v);

and add some logic to check which branch is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM