Java HashMap containsValue() Method Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 8 Likes Like Report In Java, the containsValue() method of the HashMap class is used to check whether a particular value is being mapped by a single or more than one key in the HashMap. Example 1: This example demonstrates how the containsValue() method works when String values are mapped to integer keys. Java // Java program to demonstrate // the working of containsValue() import java.util.*; public class Geeks { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hashMap = new HashMap<>(); // Mapping string values to integer keys hashMap.put(1, "Geeks"); hashMap.put(2, "For"); hashMap.put(3, "Geeks"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hashMap); // Checking for the value "Geeks" System.out.println("Is the value 'Geeks' present? " + hashMap.containsValue("Geeks")); // Checking for the value "Java" System.out.println("Is the value 'Java' present? " + hashMap.containsValue("Java")); } } OutputInitial Mappings are: {1=Geeks, 2=For, 3=Geeks} Is the value 'Geeks' present? true Is the value 'Java' present? false Syntax of containsValue() Methodpublic boolean containsValue(Object value)Parameter: The value whose presence is to be checked in the HashMap.Return Type: This method returns True if the HashMap contains at least one mapping with the specified value, otherwise returns FalseExample 2: This example shows how the containsValue() method works when integer values are mapped to string keys. Java // Java program to demonstrate // the working of containsValue() import java.util.*; public class Geeks { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hashMap = new HashMap<>(); // Mapping integer values to string keys hashMap.put("Geeks", 1); hashMap.put("For", 2); // Overwrites the value for the key "Geeks" hashMap.put("Geeks", 3); // Displaying the HashMap System.out.println("Initial Mappings are: " + hashMap); // Checking for the value 1 System.out.println("Is the value '1' present? " + hashMap.containsValue(1)); // Checking for the value 3 System.out.println("Is the value '3' present? " + hashMap.containsValue(3)); } } OutputInitial Mappings are: {Geeks=3, For=2} Is the value '1' present? false Is the value '3' present? true Note: In HashMap if you add duplicate key, the value associated with that key gets update and the previous value is removed. Comment C chinmoy lenka Follow 8 Improve C chinmoy lenka Follow 8 Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-HashMap +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like