ConcurrentHashMap computeIfAbsent() method in Java with Examples
Last Updated : 25 Jun, 2021
The computeIfAbsent(Key, Function) method of ConcurrentHashMap class which attempts to compute its value using the given mapping function for specified key if key is not already associated with a value (or is mapped to null) and enter that computed value in map else null.
- If mapping function of this method returns null, then no value is recorded for new key in map.
- This method is used to atomically update a value for given key in ConcurrentHashMap.
- If the remapping function throws an exception, the exception is rethrown, and the no mapping is recorded.
- During computation, modification this map using this method is not allowed because other threads can also uses this map.
Syntax:
public V computeIfAbsent(K key, Function<? super K, ? 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 current (existing or computed) value associated with the specified key, or null if mapping returns null.
Exception: This method throws:
NullPointerException: if the specified key or remappingFunction is null.
llegalStateException: if the computation try to do a recursive update to this map that would otherwise never complete.
RuntimeException: if the remappingFunction does so, in which case the mapping is unchanged.
Below programs illustrate the computeIfAbsent(Key, Function) method:
Program 1:
Java // Java program to demonstrate // computeIfAbsent(Key, Function) method. import java.util.concurrent.*; public class GFG { // Main method public static void main(String[] args) { // create a ConcurrentHashMap and // add some values ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("Pencil", 1000); map.put("Laptop", 55000); map.put("Clothes", 4400); map.put("Mobile", 5300); // print map details System.out.println("ConcurrentHashMap :\n " + map.toString()); // provide value for new key which is absent // using computeIfAbsent method map.computeIfAbsent("PC", k -> 60000); map.computeIfAbsent("Charger", k -> 800); // print new mapping System.out.println("new ConcurrentHashMap :\n " + map); } }
Output: ConcurrentHashMap : {Laptop=55000, Clothes=4400, Pencil=1000, Mobile=5300} new ConcurrentHashMap : {Laptop=55000, PC=60000, Clothes=4400, Pencil=1000, Charger=800, Mobile=5300}
Program 2:
Java // Java program to demonstrate // computeIfAbsent(Key, Function) method. import java.util.concurrent.*; public class GFG { // Main method public static void main(String[] args) { // create a ConcurrentHashMap and // add some values ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); map.put(1, "1000RS"); map.put(2, "5009RS"); map.put(3, "1300RS"); // print map details System.out.println("ConcurrentHashMap : \n" + map.toString()); // provide value for new key which is absent // using computeIfAbsent method map.computeIfAbsent(4, k -> "6000RS"); // this will not effect anything // because key 1 is present map.computeIfAbsent(1, k -> "8000RS"); // print new mapping System.out.println("new ConcurrentHashMap :\n" + map); } }
Output: ConcurrentHashMap : {1=1000RS, 2=5009RS, 3=1300RS} new ConcurrentHashMap : {1=1000RS, 2=5009RS, 3=1300RS, 4=6000RS}
Program 3: To show NullPointerException
Java // Java program to demonstrate NullPointerException of // computeIfAbsent(Key, Function) method. import java.util.concurrent.*; public class GFG { // Main method public static void main(String[] args) { // create a ConcurrentHashMap and // add some values ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("Pencil", 1000); map.put("Laptop", 55000); map.put("Clothes", 4400); map.put("Mobile", 5300); try { // provide value for new key which is absent // using computeIfAbsent method map.computeIfAbsent(null, k -> 60000); } catch (Exception e) { // print new mapping System.out.println("Exception: " + e); } } }
Output: Exception: java.lang.NullPointerException
References: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent-K-java.util.function.Function-
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java
My Profile