I recently tumbled upon an article which stated the importance of merge method in ConcurrentHashMap performing atomic operations. Below is the link for the article:
https://www.nurkiewicz.com/2019/03/mapmerge-one-method-to-rule-them-all.html
It says that merge method either puts new value under the given key (if absent) or updates existing key with a given value (UPSERT) with explanation of this concept. However, this is exactly what put() method does.
The get() and then put() together in ConcurrentHashMap are not thread-safe.
Please guide me to understand how is merge() taking care of this get() and put() together scenario and making operations on ConcurrentHashMap thread-safe?