There is no difference in put and replace when there is a current mapping for the required key. From replace :
Replaces the entry for the specified key only if it is currently mapped to some value.
This means that if there is already a mapping for this key, then the put and replace tags will update the map in the same way. Both will return the previous value associated with the key. However, if there is no mapping for this key, then replace will be no-op (will do nothing), while put will still update the map.
Starting with Java 8, note that you can just use
histogramType1.merge(delay, 1, Integer::sum);
This will take care of all conditions. From merge :
If the specified key is not yet associated with the value or is associated with null , associates it with the specified non-empty value. Otherwise, the associated value with the results of the given remapping function is replaced or deleted if the result is null .
In this case, we create a delay -> 1 record if the record does not exist. If it exists, it is updated by increasing the value by 1.
Tunaki
source share