A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/hashmap-containsvalue-method-in-java/ below:

Java HashMap containsValue() Method - GeeksforGeeks

Java HashMap containsValue() Method

Last Updated : 11 Jul, 2025

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"));
    }
}

Output
Initial Mappings are: {1=Geeks, 2=For, 3=Geeks}
Is the value 'Geeks' present? true
Is the value 'Java' present? false
Syntax of containsValue() Method

public boolean containsValue(Object value)

Example 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));
    }
}

Output
Initial 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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4