Java Hashtable computeIfAbsent() Method
Description
The Java Hashtable computeIfAbsent() method is used to compute a mapping for the specified key if the specified key is not 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.computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) method.
public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
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 Absent for Specific Key in a HashTable of Integer, Integer Pair Example
The following example shows the usage of Java Hashtable computeIfAbsent() 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 computeIfAbsent 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.computeIfAbsent(2, (key) -> key);
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=3, 2=2, 1=1}
Computing a Mapping if Absent for Specific Key in a HashTable of Integer, String Pair Example
The following example shows the usage of Java Hashtable computeIfAbsent() 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 computeIfAbsent() 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.computeIfAbsent(2, (key) -> {return "B";});
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=C, 2=B, 1=A}
Computing a Mapping if Absent for Specific Key in a HashTable of Integer, Object Pair Example
The following example shows the usage of Java Hashtable computeIfAbsent() 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 computeIfAbsent() 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.computeIfAbsent(2, (key) -> {return new Student(2, "Robert");});
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 ], 2=[ 2, Robert ], 1=[ 1, Julie ]}