How can I execute a function by the value of the card only if it is present, without making any changes to the card? I want to do this using the declarative style of "Java 8", comparable to Optional.ifPresent().
My use case is as follows:
I receive updates (new or deleted) for objects partially, I want to register these updates with my parent. For accounting, I have the following:
Map<ParentId, Parent> parents = ...
When I get a new baby, I do the following:
parents.computeIfAbsent(child.getParentId(), k -> new Parent()).addChild(child));
However, for deletion, I cannot find a declarative function. Straight ahead, I would implement this as:
if(parents.containsKey(child.getParentId())
{
parents.get(child.getParentId()).removeChild(child);
}
Or I could wrap the value in Optional:
Optional.ofNullable(parents.get(child.getParentId()).ifPresent(p -> p.removeChild(child));
, Parent , , . , ( removeChild() Parent):
parents.computeIfPresent(child.getParentId(), (k, v) -> v.removeChild());
, Optional.ifPresent()?