HashTable compute() method in Java with Examples Last Updated : 02 Jun, 2023 Comments Improve Suggest changes Like Article Like Report The compute(Key, BiFunction) method of Hashtable class allows to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). If the remapping function passed in compute() of Hashtable returns null as a return value then the mapping is removed from Hashtable(or remains absent if initially absent).If the remapping function throws an exception, the exception is rethrown, and the current mapping is left unchanged.During computation, modification this map using this method is not allowed.The compute() method can be used to update an existing value inside Hashtable. For example, This mapping append string value of mapping:Hashtable.compute(key, (k, v) -> v.append("strValue"))This method will throw a ConcurrentModificationException if the remapping function modified this map during computation. Syntax: public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Parameters: This method accepts two parameters: key: key with which the value is to be associated.remappingFunction: function to do the operation on value. Returns: This method returns new value associated with the specified key, or null if none. Exception: This method throws: ConcurrentModificationException: if it is detected that the remapping function modified this map. Below programs illustrate the compute(Key, BiFunction) method: Program 1: Java // Java program to demonstrate // compute(Key, BiFunction) method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // create a table and add some values Map<String, Integer> table = new Hashtable<>(); table.put("Pen", 10); table.put("Book", 500); table.put("Clothes", 400); table.put("Mobile", 5000); // print map details System.out.println("hashTable: " + table.toString()); // remap the values of hashTable // using compute method table.compute("Pen", (key, val) -> val + 15); table.compute("Clothes", (key, val) -> val - 120); // print new mapping System.out.println("new hashTable: " + table); } } Output:hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400} new hashTable: {Book=500, Mobile=5000, Pen=25, Clothes=280} Program 2: Java // Java program to demonstrate // compute(Key, BiFunction) method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // create a table and add some values Map<Integer, String> table = new Hashtable<>(); table.put(1, "100RS"); table.put(2, "500RS"); table.put(3, "1000RS"); // print map details System.out.println("hashTable: " + table.toString()); // remap the values of hashTable // using compute method table.compute(3, (key, val) -> val.substring(0, 4) + "00RS"); table.compute(2, (key, val) -> val.substring(0, 2) + "$"); // print new mapping System.out.println("new hashTable: " + table); } } Output:hashTable: {3=1000RS, 2=500RS, 1=100RS} new hashTable: {3=100000RS, 2=50$, 1=100RS} References: https://docs.oracle.com/javase/10/docs/api/java/util/Hashtable.html#compute(K, java.util.function.BiFunction) Comment More infoAdvertise with us Next Article HashTable compute() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package java-basics Java-Functions Java-HashMap +2 More Practice Tags : JavaJava-Collections Similar Reads Hashtable computeIfAbsent() method in Java with Examples The computeIfAbsent(Key, Function) method of Hashtable class which allows you to compute value of a mapping for specified key if key is not already associated with a value (or is mapped to null). If mapping function of this method returns null, then no mapping is recorded. If the remapping function 2 min read CompositeName hashCode() method in Java with Examples The hashCode() method of a javax.naming.CompositeName class is used to return the hash code of this composite name. The hash code of this composite name object is the sum of the hash codes of the "canonicalized" forms of individual components of this composite name. Syntax: public int hashCode() Par 1 min read Double hashCode() method in Java with examples The hashCode() method of Double class is a built-in method use to return the hashcode value of this Double object. Syntax: DoubleObject.hashCode() Parameters: It takes no parameters. Return Type: It returns an int value. The value returned is (int)(v^(v>>>32)) where v is a long variable equ 1 min read Byte hashCode() method in Java with examples The hashCode() method of Byte class is a built in method in Java which is used to return the hash code of the ByteObject. Note: The hashCode() returns the same value as intValue(). Syntax ByteObject.hashCode() Return Value: It return an int value which represents the hashcode of the specified Byte o 2 min read Date hashCode() method in Java with Examples The hashCode() method of Java Date class returns a value which is a hash code for this object. Syntax: public int hashCode() Parameters: The function does not accept any parameter. Return Value: It returns a hashCode value for this object. Exception: The function does not throws any exception. Progr 2 min read Hashtable keySet() Method in Java with Examples The java.util.Hashtable is used to create a set of key elements in the hash table. It basically returns a set view of the keys, or we can create a new set and store the key elements in them. Syntax: public Set<K> keySet() K : type of the Keys in the hash table Parameters: The method does not t 2 min read HashTable forEach() method in Java with Examples The forEach(BiConsumer) method of Hashtable class perform the BiConsumer operation on each entry of hashtable until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of key-value pair of hashtable performed in the order of iteration. 2 min read Constructor hashCode() method in Java with Examples The hashCode() method of java.lang.reflect.Constructor class is used to return a hashcode for this Constructor object. The hashcode is always the same if the constructed object doesnât change. Hashcode is a unique code generated by the JVM at the time of class object creation. We can use hashcode to 2 min read BitSet hashCode Method in Java with Examples The hashCode() Method of BitSet class in Java is used to fetch the hash code value of a particular this BitSet. This returned hash codes value depends on the set bits of the bit set being passed. Syntax: BitSet.hashCode() Parameters: The method does not accept any parameters. Return Value: The metho 2 min read ChronoPeriod hashCode() method in Java with Examples The hashCode() method of ChronoPeriod class in Java is used to get the generated hashCode for this period. Syntax: int hashCode() Parameters: This method does not accepts any parameter. Return Value: This method returns the hashCode generated for the given period. Below programs illustrate the hashC 2 min read Like