Java Hashtable computeIfPresent() Method



Description

The Java Hashtable 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 hashtable unless null.

Declaration

Following is the declaration for java.util.Hashtable.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 hashtable.

Computing a Mapping if Present for Specific Key in a HashTable of Integer, Integer Pair Example

The following example shows the usage of Java Hashtable computeIfPresent() method to get updated values of a Hashtable. We've created two Hashtable objects of Integer,Integer pairs. Then few entries are added to hashtable, then using computeIfPresent() method, the values are updated and then updated hashtable is printed.

 package com.tutorialspoint; import java.util.Hashtable; public class HashtableDemo { public static void main(String args[]) { // create two hash tables Hashtable<Integer, Integer> hashtable = new Hashtable<>(); // populate hashtable hashtable.put(1, 1); hashtable.put(3, 3); // print the hashtable System.out.println("Hashtable: " + hashtable); // update the values of the hashtable hashtable.computeIfPresent(1, (key,value) -> value * 10); hashtable.computeIfPresent(2, (key,value) -> value * 20); hashtable.computeIfPresent(3, (key,value) -> value * 30); System.out.println("Updated Hashtable: " + hashtable); } } 

Output

Let us compile and run the above program, this will produce the following result.

 Hashtable: {3=3, 1=1} Updated Hashtable: {3=90, 1=10} 

Computing a Mapping if Present for Specific Key in a HashTable of Integer, String Pair Example

The following example shows the usage of Java Hashtable compute() method to get updated values of a Hashtable. We've created two Hashtable objects of Integer,String. Then few entries are added to hashtable, then using computeIfPresent() method, the values are updated and then updated hashtable is printed.

 package com.tutorialspoint; import java.util.Hashtable; public class HashtableDemo { public static void main(String args[]) { // create two hash tables Hashtable<Integer, String> hashtable = new Hashtable<>(); // populate hashtable hashtable.put(1, "A"); hashtable.put(3, "C"); // print the hashtable System.out.println("Hashtable: " + hashtable); // update the values of the hashtable hashtable.computeIfPresent(1, (key,value) -> value.concat("123")); hashtable.computeIfPresent(2, (key,value) -> value.concat("456")); hashtable.computeIfPresent(3, (key,value) -> value.concat("789")); System.out.println("Updated Hashtable: " + hashtable); } } 

Output

Let us compile and run the above program, this will produce the following result.

 Hashtable: {3=C, 1=A} Updated Hashtable: {3=C789, 1=A123} 

Computing a Mapping if Present for Specific Key in a HashTable of Integer, Object Pair Example

The following example shows the usage of Java Hashtable compute() method to get a update values of a Hashtable. We've created two Hashtable objects of Integer,Student pairs. Then few entries are added to hashtable, then using computeIfPresent() method, the values are updated and then updated hashtable is printed.

 package com.tutorialspoint; import java.util.Hashtable; public class HashtableDemo { public static void main(String args[]) { // create two hash tables Hashtable<Integer, Student> hashtable = new Hashtable<>(); // populate hashtable hashtable.put(1, new Student(1, "Julie")); hashtable.put(3, new Student(3, "Adam")); // print the hashtable System.out.println("Hashtable: " + hashtable); // update the values of the hashtable hashtable.computeIfPresent(1, (key,value) -> value.update("Roberts")); hashtable.computeIfPresent(2, (key,value) -> value.update("Pitts")); hashtable.computeIfPresent(3, (key,value) -> value.update("Cruise")); System.out.println("Updated Hashtable: " + hashtable); } } 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.

 Hashtable: {3=[ 3, Adam ], 1=[ 1, Julie ]} Updated Hashtable: {3=[ 3, Adam Cruise ], 1=[ 1, Julie Roberts ]} 
java_util_hashtable.htm
Advertisements