JDK-8174087 : Implementation requirements code example for ConcurrentMap.putIfAbsent() returns incorrect value
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Affected Version: 8
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2017-01-31
  • Updated: 2017-03-08
  • Resolved: 2017-02-07
Related Reports
Duplicate :  
Duplicate :  
Description
A DESCRIPTION OF THE PROBLEM :
The description of both ConcurrentMap.computeIfAbsent and Map.computeIfAbsent state that the return value is the current (existing or computed) value mapped for the given key.

However, the "equivalent code" snippet in the Implementation Requirements section:
 if (map.get(key) == null) {
     V newValue = mappingFunction.apply(key);
     if (newValue != null)
         return map.putIfAbsent(key, newValue);
 }

Shows it returning the result of putIfAbsent(), which is defined to return the previous mapping, or null if none existed.



URL OF FAULTY DOCUMENTATION :
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#computeIfAbsent-K-java.util.function.Function-


Comments
This issue with the documentation has been fixed in JDK 9 with JDK-8145164.
07-02-2017