I have been studying leetcode problems and ran into one where I needed to increment the value of an existing key/value pair in a HashMap. I found the consensus was that the best way to do this in Java 8 is the following piece of code:
myMap.computeIfPresent(key, (k, v) -> v + 1);
In this example, why does v++
fail to work in place of v + 1
?
Secondly, where might I go besides asking the question here to figure this out? I got the code to work but want to understand why my first instinct to put v++
doesn't.