The merge() method associates the specified value to the specified key if the specified key is not already associated. However, if the specified key is already associated with a value, it will merge the new specified value with the existing old value. For example,
Output
Other methods of HashMap.
In the above example, the merge() method takes 3 parameters: key, newValue and a lambda expression (that computes the new merged value).