Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 compute() and computeIfPresent() to check an existing value

I have this piece of code :

if (notificationSend.get(key) != null && notificationSend.get(key).equals(value)) {
   return true;
} else {
   notificationSend.put(key, value);
   return false;
}

and I want to know if it is possible to refactor it using Jav8 Enhancements like compute() , computeIfPresent() or computeIfAbsent()

like image 750
Nunyet de Can Calçada Avatar asked Oct 21 '18 19:10

Nunyet de Can Calçada


1 Answers

Assuming value is non-null, you don't need to use a conditional, or any of those compute* methods.

ValueType oldValue = map.put(key, value);
return value.equals(oldValue);
like image 56
Andy Turner Avatar answered Oct 20 '22 00:10

Andy Turner