Open In App

ConcurrentHashMap containsKey() Method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

The containsKey() method in Java’s ConcurrentHashMap class is used to determine whether the map contains a given key. It has the following signature:

boolean containsKey(Object key)

where:

key is the key to be searched for in the map.

The containsKey() method works in a concurrent environment, which means that it can be called from multiple threads without causing any data race or synchronization issues.

When a containsKey() operation is performed, the method first acquires the lock on the segment of the map corresponding to the given key and then checks if the key is present in the map. If the key is found, the method returns true. Otherwise, it returns false.

Here is an example of using the containsKey() method:

Java




import java.util.concurrent.ConcurrentHashMap;
 
public class ConcurrentHashMapExample {
    public static void main(String[] args)
    {
        ConcurrentHashMap<String, Integer> map
            = new ConcurrentHashMap<>();
        map.put("Alice", 25);
        map.put("Bob", 30);
        map.put("Charlie", 35);
 
        boolean result = map.containsKey("Bob");
        System.out.println("Map contains key Bob: "
                           + result); // true
 
        result = map.containsKey("Dave");
        System.out.println("Map contains key Dave: "
                           + result); // false
    }
}


Output

Map contains key Bob: true
Map contains key Dave: false

In this example, we first create a ConcurrentHashMap and add three key-value pairs to it using the put() method.

Next, we perform a containsKey() operation to check if the key “Bob” is present in the map. The method returns true, and we print out the result.

Finally, we perform a containsKey() operation to check if the key “Dave” is present in the map. The method returns false, and we print out the result.

The java.util.concurrent.ConcurrentHashMap.containsKey() method is an in-built function in Java that accepts a parameter and checks whether it is key in this map. 

Syntax:

chm.containsKey(Object key_element)

Parameters: The method accepts a single parameter key_element of an object type which is to be checked for whether it is a key or not. 

Return Value: The method returns true if the specified key_element is a key of this map and false otherwise. 

Exception: The function throws NullPointerException when the specified key_element is null. 

The below programs illustrate the use of java.util.concurrent.ConcurrentHashMap.containsKey() method:

Program 1: This program involves mapping String Values to Integer Keys. 

Java




/* Java Program Demonstrate containsKey()
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");
 
        // Checking whether 105 is a key of the map
        if (chm.containsKey(105)) {
            System.out.println("105 is a key.");
        }
        else {
            System.out.println("105 is not a key.");
        }
 
        // Checking whether 100 is a key of the map
        if (chm.containsKey(100)) {
            System.out.println("100 is a key.");
        }
        else {
            System.out.println("100 is not a key.");
        }
    }
}


Output

105 is not a key.
100 is a key.

Program 2: This program involves mapping Integer Values to String Keys. 

Java




/* Java Program Demonstrate containsKey()
method of ConcurrentHashMap */
 
import java.util.concurrent.*;
class ConcurrentHashMapDemo {
    public static void main(String[] args)
    {
        ConcurrentHashMap<String, Integer> chm
            = new ConcurrentHashMap<String, Integer>();
        chm.put("Geeks", 120);
        chm.put("for", 11);
        chm.put("GeeksforGeeks", 15);
        chm.put("Gfg", 50);
        chm.put("GFG", 25);
 
        // Checking whether GFG is a key of the map
        if (chm.containsKey("GFG")) {
            System.out.println("GFG is a key.");
        }
        else {
            System.out.println("GFG is not a key.");
        }
 
        // Checking whether Geek is a key of the map
        if (chm.containsKey("Geek")) {
            System.out.println("Geek is a key.");
        }
        else {
            System.out.println("Geek is not a key.");
        }
    }
}


Output

GFG is a key.
Geek is not a key.


Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads