Whether to overwrite value | return value | |
put | Yes | Before covering |
compute | Yes | After covering |
putIfAbsent | no | Before covering |
computeIfAbsent | no | After covering |
to sum up:
1. put与compute:
Regardless of whether the key exists, it is mandatory to overwrite it with value.
Difference: put returns the old value or null, compute returns the new value
2. putIfAbsent and computeIfAbsent:
If the key exists, no operation is performed. If the key does not exist, a new pair of (key, value) is assigned.
Difference: putIfAbsent returns the old value or null, computeIfAbsent returns the new value