
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java HashMap computeIfPresent() Method
Description
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.
Declaration
Following 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 Value
The method call returns the new value associated with the specified key, or null if none.
Exception
ConcurrentModificationException − if it is detected that the remapping function modified this map.
Compute Mapping of a mapped key of a HashMap of Integer, Integer Pair Example
The 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 ]}