Open In App
Related Articles

ConcurrentHashMap containsValue() Method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

boolean containsValue(Object value)

where:

  1. value is the value to be searched for in the map.
  2. The containsValue() method works in a concurrent environment, which means that it can be called from multiple threads
  3. without causing any data race or synchronization issues.

When a containsValue() operation is performed, the method first acquires the lock on all the segments of the map and then searches for the given value in the values of all the entries in the map. If the value is found in any of the entries, the method returns true. Otherwise, it returns false.

Here is an example of using the containsValue() 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.containsValue(30);
        System.out.println("Map contains value 30: " + result); // true
 
        result = map.containsValue(40);
        System.out.println("Map contains value 40: " + result); // false
    }
}

                    

Output
Map contains value 30: true
Map contains value 40: 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 containsValue() operation to check if the value 30 is present in the map. The method returns true, and we print out the result.

Finally, we perform a containsValue() operation to check if the value 40 is present in the map. The method returns false, and we print out the result.

The java.util.concurrent.ConcurrentHashMap.containsValue() method is an in-built function in Java that accepts a value and returns true if one or more keys are mapped to the specified value. This method traverses the entire hashtable. Thus it is a much slower function than containsKey() method. 

Syntax:

chm.containsValue(Object val_element)

Parameters: The method accepts a single parameter val_element of an object type which is to be checked for whether it is mapped to any key in the map or not. 

Return Value: The method returns true if the specified val_element is mapped to any key in this map and false otherwise. 

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

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

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

Java

/* Java Program to demonstrate containsValue()
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);
 
        // Check whether a key is mapped to 100
        if (chm.containsValue(100)) {
            System.out.println("100 is mapped.");
        }
        else {
            System.out.println("100 is not mapped.");
        }
 
        // Check whether a key is mapped to 120
        if (chm.containsValue(120)) {
            System.out.println("120 is mapped.");
        }
        else {
            System.out.println("120 is not mapped.");
        }
    }
}

                    

Output
100 is not mapped.
120 is mapped.

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

Java

/* Java Program to demonstrate containsValue()
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");
 
        // Check whether a key is mapped to Geeks
        if (chm.containsValue("Geeks")) {
            System.out.println("Geeks is mapped.");
        }
        else {
            System.out.println("Geeks is not mapped.");
        }
 
        // Check whether a key is mapped to GfG
        if (chm.containsValue("GfG")) {
            System.out.println("GfG is mapped.");
        }
        else {
            System.out.println("GfG is not mapped.");
        }
    }
}

                    

Output
Geeks is mapped.
GfG is not mapped.


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