compute(K, BiFunction): This method is available in java.util.TreeMap class of Java.
Syntax:
String java.util.TreeMap.compute(K key, BiFunction<? super K, ? super V, ? extends V > remappingFunction)
This method takes two arguments. This method attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
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.TreeMap;import java.util.function.BiFunction;public class TreeMapcompute {public static void main(String[] args) {TreeMap<Integer, String> treeMap =new TreeMap<Integer, String>();treeMap.put(2, "Hello");treeMap.put(11, "Java");treeMap.put(23, "Program");treeMap.put(6, "C++");treeMap.put(25, "Program");Integer key = 2;BiFunction<? super Integer, ? super String, ?extends String> remappingFunction = (a,b)->a+b;System.out.println(treeMap.compute(key,remappingFunction));}}
Output:
2Hello
No comments:
Post a Comment