The Java HashMap computeIfPresent() method is used to compute a mapping for the specified key if the specified key is already associated with a value (or is mapped to null), using the given mapping function and enters it into this map unless null.
DeclarationFollowing is the declaration for java.util.HashMap.computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) method.
public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)Parameters
key − key with which the specified value is to be associated
remappingFunction − the remapping function to compute a value
Return ValueThe method call returns the new value associated with the specified key, or null if none.
ExceptionConcurrentModificationException − if it is detected that the remapping function modified this map.
Compute Mapping of a mapped key of a HashMap of Integer, Integer Pair ExampleThe following example shows the usage of Java HashMap computeIfPresent() method to get updated values of a Map. We've created two Map objects of Integer,Integer pair. Then few entries are added to map, then using computeIfPresent() method, the values are updated and then updated map is printed.
package com.tutorialspoint; import java.util.HashMap; public class HashMapDemo { public static void main(String args[]) { // create two hash maps HashMap<Integer, Integer> newmap = new HashMap<>(); // populate map newmap.put(1, 1); newmap.put(3, 3); // print the map System.out.println("Map: " + newmap); // update the values of the map newmap.computeIfPresent(1, (key,value) -> value * 10); newmap.computeIfPresent(2, (key,value) -> value * 20); newmap.computeIfPresent(3, (key,value) -> value * 30); System.out.println("Updated Map: " + newmap); } }Output
Let us compile and run the above program, this will produce the following result.
Map: {1=1, 3=3} Updated Map: {1=10, 3=90}Compute Mapping of unmapped key of a HashMap of Integer, String Pair Example
The following example shows the usage of Java HashMap compute() method to get updated values of a Map. We've created two Map objects of Integer,String pair. Then few entries are added to map, then using computeIfPresent() method, the values are updated and then updated map is printed.
package com.tutorialspoint; import java.util.HashMap; public class HashMapDemo { public static void main(String args[]) { // create two hash maps HashMap<Integer, String> newmap = new HashMap<>(); // populate map newmap.put(1, "A"); newmap.put(3, "C"); // print the map System.out.println("Map: " + newmap); // update the values of the map newmap.computeIfPresent(1, (key,value) -> value.concat("123")); newmap.computeIfPresent(2, (key,value) -> value.concat("456")); newmap.computeIfPresent(3, (key,value) -> value.concat("789")); System.out.println("Updated Map: " + newmap); } }Output
Let us compile and run the above program, this will produce the following result.
Map: {1=A, 2=B, 3=C} Updated Map: {1=A123, 2=B456, 3=C789}Compute Mapping of unmapped key of a HashMap of Integer, Student Pair Example
The following example shows the usage of Java HashMap compute() method to get a update values of a Map. We've created two Map objects of Integer,Student pair. Then few entries are added to map, then using computeIfPresent() method, the values are updated and then updated map is printed.
package com.tutorialspoint; import java.util.HashMap; public class HashMapDemo { public static void main(String args[]) { // create two hash maps HashMap<Integer, Student> newmap = new HashMap<>(); // populate map newmap.put(1, new Student(1, "Julie")); newmap.put(3, new Student(3, "Adam")); // print the map System.out.println("Map: " + newmap); // update the values of the map newmap.computeIfPresent(1, (key,value) -> value.update("Roberts")); newmap.computeIfPresent(2, (key,value) -> value.update("Pitts")); newmap.computeIfPresent(3, (key,value) -> value.update("Cruise")); System.out.println("Updated Map: " + newmap); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } public Student update(String surname) { this.name = this.name.concat(" " + surname); return this; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }Output
Let us compile and run the above program, this will produce the following result.
Map: {1=[ 1, Julie ], 3=[ 3, Adam ]} Updated Map: {1=[ 1, Julie Roberts ], 3=[ 3, Adam Cruise ]}
java_util_hashmap.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4