The java.util.concurrent.ConcurrentHashMap.putIfAbsent() is an in-built function in Java which accepts a key and a value as parameters and maps them if the specified key is not mapped to any value.
Syntax:
chm.putIfAbsent(key_elem, val_elem)
Parameters: The function accepts two parameters which are described below:
- key_elem: This parameter specifies the key to which the specified val_elem is to be mapped if key_elem is not associated with any value.
- val_elem: This parameter specifies the value to be mapped to the specified key_elem.
Return Value: The function returns the existing value mapped to the key and returns null if no value is previously mapped to the key.
Exception: The function throws NullPointerException when the specified parameters are null.
Below programs illustrate the ConcurrentHashMap.putIfAbsent() method :
Program 1: An existing key is passed as parameter to the function.
// Java Program Demonstrate putIfAbsent() // method of ConcurrentHashMap import java.util.concurrent.*; class ConcurrentHashMapDemo { public static void main(String[] args) { ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>(); chm.put(100, "Geeks"); chm.put(101, "for"); chm.put(102, "Geeks"); chm.put(103, "Gfg"); chm.put(104, "GFG"); // Displaying the HashMap System.out.println("Initial Mappings are: " + chm); // Inserting non-existing key along with value String returned_value = (String)chm.putIfAbsent(108, "All"); // Verifying the returned value System.out.println("Returned value is: " + returned_value); // Displayin the new map System.out.println("New mappings are: " + chm); } } |
Initial Mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}
Returned value is: null
New mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG, 108=All}
Program 2: A non-existing key is passed as parameter to the function.
// Java Program Demonstrate putIfAbsent() // method of ConcurrentHashMap import java.util.concurrent.*; class ConcurrentHashMapDemo { public static void main(String[] args) { ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>(); chm.put(100, "Geeks"); chm.put(101, "for"); chm.put(102, "Geeks"); chm.put(103, "Gfg"); chm.put(104, "GFG"); // Displaying the HashMap System.out.println("Initial Mappings are: " + chm); // Inserting existing key along with value String returned_value = (String)chm.putIfAbsent(100, "All"); // Verifying the returned value System.out.println("Returned value is: " + returned_value); // Displayin the new map System.out.println("New mappings are: " + chm); } } |
Initial Mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}
Returned value is: Geeks
New mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#putIfAbsent()
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- HashTable putIfAbsent() method in Java with Examples
- HashMap putIfAbsent(key, value) method in Java with Examples
- ConcurrentHashMap put() method in Java
- ConcurrentHashMap remove() method in Java
- ConcurrentHashMap containsValue() Method in Java
- ConcurrentHashMap containsKey() Method in Java
- ConcurrentHashMap get() Method in Java
- ConcurrentHashMap size() Method in Java
- ConcurrentHashMap isEmpty() Method in Java
- ConcurrentHashMap putAll() method in Java
- ConcurrentHashMap keys() method in Java with Examples
- ConcurrentHashMap elements() method in Java with Examples
- ConcurrentHashMap computeIfAbsent() method in Java with Examples
- ConcurrentHashMap compute() method in Java with Examples
- ConcurrentHashMap entrySet() method in Java with Examples
- ConcurrentHashMap contains() method in Java with Examples
- ConcurrentHashMap values() method in Java with Examples
- ConcurrentHashMap keySet() method in Java with Examples
- ConcurrentHashMap in Java
- Java ConcurrentHashMap | clear()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

