Java basic summary (eighty-five)-the difference between put and compute, computeIfAbsent and putIfAbsent of the new features of java8

Original link

  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

Guess you like

Origin blog.csdn.net/lsx2017/article/details/114004974