Description: The java.util.HashMap class provides a containsValue() method which allows us to check whether a specific value is present in the HashMap or not. This method returns true if the specified value is present in the HashMap and false otherwise.
Code Example:
// Creating a HashMap object HashMap map = new HashMap();
// Adding key-value pairs to the HashMap map.put("One", 1); map.put("Two", 2); map.put("Three", 3);
// Checking if the value 3 is present in the HashMap if (map.containsValue(3)) { System.out.println("The value 3 is present in the HashMap"); } else { System.out.println("The value 3 is not present in the HashMap"); }
// Checking if the value 4 is present in the HashMap if (map.containsValue(4)) { System.out.println("The value 4 is present in the HashMap"); } else { System.out.println("The value 4 is not present in the HashMap"); }
Brief Explanation: In the above code example, we create a new HashMap object, add key-value pairs to it, and then use the containsValue() method to check if the given value is present or not. In the first check, we search for value 3 which is present in the HashMap, so the output will be "The value 3 is present in the HashMap". In the second check, we search for value 4 which is not present in the HashMap, so the output will be "The value 4 is not present in the HashMap".
Package Library: The containsValue() method is provided by the java.util package which is a part of the Java Core API.
Java HashMap.containsValue - 30 examples found. These are the top rated real world Java examples of java.util.HashMap.containsValue extracted from open source projects. You can rate examples to help us improve the quality of examples.