The java.util.concurrent.ConcurrentHashMap.containsValue() method is an in-built function in Java which 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 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.
Below programs illustrate the use of java.util.concurrent.ConcurrentHashMap.containsValue() method :
Program 1: This program involves mapping Integer Values to String Keys.
/* 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."); } } } |
100 is not mapped. 120 is mapped.
Program 2: This program involves mapping String Values to Integer Keys.
/* 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."); } } } |
Geeks is mapped. GfG is not mapped.
Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#containsValue()
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:
- Java.util.TreeMap.containskey() and containsValue() in Java
- HashMap containsValue() Method in Java
- Hashtable containsValue() Method in Java
- TreeMap containsValue() Method in Java
- EnumMap containsValue(value) method in Java
- WeakHashMap containsValue() Method in Java
- IdentityHashMap containsValue() Method in Java
- AbstractMap containsValue() Method in Java with Examples
- Map containsValue() method in Java with Examples
- ConcurrentSkipListMap containsValue() method in Java with Examples
- Properties containsValue(value) method in Java with Examples
- ConcurrentHashMap put() method in Java
- ConcurrentHashMap remove() 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 putIfAbsent() Method in Java
- ConcurrentHashMap keys() method in Java with Examples
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.

