combine putIfAbsent and replace with ConcurrentMap
问题 I have a usecase where I have to insert a new value if the key does not exist in the ConcurrentHashMap replace the old value with a new value if the key already exists in the ConcurrentHashMap, where the new value is derived from the old value (not an expensive operation) I've the following code to offer: public void insertOrReplace(String key, String value) { boolean updated = false; do { String oldValue = concurrentMap.get(key); if (oldValue == null) { oldValue = concurrentMap.putIfAbsent