Java Hashtable compute() Method



Description

The Java Hashtable compute() method is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). For example, to either create or append a String msg to a value mapping.

Declaration

Following is the declaration for java.util.Hashtable.compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) method.

 public V compute(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 for Specific Key in a HashTable of Integer, Integer 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,Integer pairs. Then few entries are added to hashtable, then using compute 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(2, 2); hashtable.put(3, 3); // print the hashtable System.out.println("Hashtable: " + hashtable); // update the values of the hashtable hashtable.compute(1, (key,value) -> value * 10); hashtable.compute(2, (key,value) -> value * 20); hashtable.compute(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, 2=2, 1=1} Updated Hashtable: {3=90, 2=40, 1=10} 

Computing a Mapping 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 pairs. Then few entries are added to hashtable, then using compute 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(2, "B"); hashtable.put(3, "C"); // print the hashtable System.out.println("Hashtable: " + hashtable); // update the values of the hashtable hashtable.compute(1, (key,value) -> value.concat("123")); hashtable.compute(2, (key,value) -> value.concat("456")); hashtable.compute(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, 2=B, 1=A} Updated Hashtable: {3=C789, 2=B456, 1=A123} 

Computing a Mapping 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 compute 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(2, new Student(2, "Robert")); hashtable.put(3, new Student(3, "Adam")); // print the hashtable System.out.println("Hashtable: " + hashtable); // update the values of the hashtable hashtable.compute(1, (key,value) -> value.update("Roberts")); hashtable.compute(2, (key,value) -> value.update("Pitts")); hashtable.compute(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 ], 2=[ 2, Robert ], 1=[ 1, Julie ]} Updated Hashtable: {3=[ 3, Adam Cruise ], 2=[ 2, Robert Pitts ], 1=[ 1, Julie Roberts ]} 
java_util_hashtable.htm
Advertisements