Why does map.merge() not call the remapping function every time?

The problem is here

(k, v) -> v + 1

You should be doing

(k, v) -> k + v

If you check the implementation of merge it says, remappingFunction.apply(oldValue, value); means the existing value will be the first parameter in which you should add the same number you initialized it with which comes as a second parameter for that function.

Update


Completing @Mritunjay answer's, here's an equivalent using compute where you might be able to see the difference:

fruitCounts.compute(fruit, (k,v) -> v == null ? 1 : v + 1) //computing over the value
fruitCounts.merge(fruit, 1, (oldValue, newValue) -> oldValue + 1) //merging over the value