computeIfPresent(K, BiFunction): This method is available in java.util.HashMap class of Java.
Syntax:
V java.util.HashMap.computeIfPresent(K key, BiFunction<? super String, ? super Integer, ? extends Integer> remappingFunction)
This method takes two arguments. If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
Parameters: Two parameters are required for this method.
key: key with which the specified value is to be associated.
remappingFunction: the remapping function to compute a value.
Returns: the new value associated with the specified key, or null if none.
Throws:
ConcurrentModificationException - if it is detected that the remapping function modified this map
Approach
Java
import java.util.HashMap;import java.util.function.BiFunction;public class HashMapcomputeIfPresent {public static void main(String[] args) {HashMap<String, Integer> hashMap =new HashMap<String, Integer>();hashMap.put("Hello", 1);hashMap.put("World", 2);String key = "123";BiFunction<? super String, ? super Integer,? extends Integer> remappingFunction =(a, b) -> Integer.parseInt(a)+ 12;System.out.println(hashMap.computeIfPresent(key,remappingFunction));}}
Output:
null
No comments:
Post a Comment