【发布时间】:2014-10-21 14:28:46
【问题描述】:
ConcurrentMap 的 JavaDoc - putIfAbsent 说:
/**
* If the specified key is not already associated
* with a value, associate it with the given value.
* This is equivalent to
*
* if (!map.containsKey(key))
* return map.put(key, value);
* else
* return map.get(key);
*
* except that the action is performed atomically.
*/
但是 putIfAbsent(来自 ConcurrentMap)和 put(来自 Map)都返回前一个值(或者当没有前一个值时返回 null)。因此在示例中,当 map 不包含 key 时,返回 null。这是代码的正确意图吗?改为返回“值”不是更好吗?
【问题讨论】:
-
你已经有价值了。
标签: java