Java HashMap containsValue() Method

The HashMap.containsValue() method in Java is used to check if a specific value is present in a HashMap.

Table of Contents

  1. Introduction
  2. containsValue Method Syntax
  3. Examples
    • Checking for the Presence of Values in a HashMap
    • Real-World Use Case: Verifying Employee Names
  4. Conclusion

Introduction

The HashMap.containsValue() method is a member of the HashMap class in Java. It allows you to check if a specific value exists in the HashMap. This can be useful when you need to verify the presence of a value before performing operations that depend on the presence of that value.

containsValue() Method Syntax

The syntax for the containsValue method is as follows:

public boolean containsValue(Object value)
  • The method takes a single parameter value of type Object, which represents the value to be checked for presence in the HashMap.
  • The method returns a boolean value:
    • true if the HashMap contains one or more keys mapping to the specified value.
    • false if the HashMap does not contain the specified value.

Examples

Checking for the Presence of Values in a HashMap

The containsValue method can be used to check if a specific value is present in a HashMap.

Example

import java.util.HashMap;
import java.util.Map;

public class ContainsValueExample {
    public static void main(String[] args) {
        // Creating a Map with String keys and String values
        Map<String, String> countries = new HashMap<>();

        // Adding entries to the HashMap
        countries.put("India", "IN");
        countries.put("United States", "US");
        countries.put("Canada", "CA");

        // Checking for the presence of values in the HashMap
        boolean containsIN = countries.containsValue("IN");
        boolean containsBR = countries.containsValue("BR");

        // Printing the results
        System.out.println("HashMap contains value 'IN': " + containsIN);
        System.out.println("HashMap contains value 'BR': " + containsBR);
    }
}

Output:

HashMap contains value 'IN': true
HashMap contains value 'BR': false

Real-World Use Case: Verifying Employee Names

In a real-world scenario, you might use the containsValue method to verify if an employee name exists in a company’s employee database before performing operations such as updating employee details or processing payroll.

Example

import java.util.HashMap;
import java.util.Map;

public class EmployeeNameVerification {
    public static void main(String[] args) {
        // Creating a Map with String keys (employee IDs) and String values (employee names)
        Map<String, String> employeeDatabase = new HashMap<>();

        // Adding entries to the HashMap
        employeeDatabase.put("E001", "Ravi Kumar");
        employeeDatabase.put("E002", "Priya Sharma");
        employeeDatabase.put("E003", "Vijay Singh");

        // Employee name to be searched
        String employeeNameToSearch = "Priya Sharma";

        // Checking if the employee name exists in the database
        if (employeeDatabase.containsValue(employeeNameToSearch)) {
            System.out.println("Employee name " + employeeNameToSearch + " exists in the database.");
        } else {
            System.out.println("Employee name " + employeeNameToSearch + " does not exist in the database.");
        }
    }
}

Output:

Employee name Priya Sharma exists in the database.

Conclusion

The HashMap.containsValue() method in Java provides a way to check if a specific value is present in a HashMap. By understanding how to use this method, you can efficiently verify the presence of values and make decisions based on their existence. This method is useful when you need to ensure that a value exists before performing operations that depend on its presence, such as updating records or processing transactions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top