Compute a new value for an existing entry in a map if the entry’s key is present and its current value is not null.
import public
|
The computeIfPresent() method computes a value for an entry based on its key. If an entry with the specified key does not exist or its value is null, the map remains unchanged.
The value is computed using a function, which can be defined with a lambda expression compatible with Java’s BiFunction interface’s apply() method.
public |
K represents the data type of the map’s keys.
Parameter |
Description |
key |
Required: Specifies the key for the entry. |
function |
Required: A Function object or lambda expression that computes the value of the entry. The function’s first parameter holds the entry’s key, while the second parameter holds its value. |
Returns: |
The computed value by the function or null if there is no change in the map. |