Java HashMap containsValue() Method

The HashMap.containsValue() method in Java is used to check if a specific value is present in a HashMap. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

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

Introduction

The 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 associated with 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;

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

        // Adding entries to the HashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Checking for the presence of values in the HashMap
        boolean contains25 = people.containsValue(25);
        boolean contains40 = people.containsValue(40);

        // Printing the results
        System.out.println("HashMap contains value 25: " + contains25);
        System.out.println("HashMap contains value 40: " + contains40);
    }
}

Output:

HashMap contains value 25: true
HashMap contains value 40: false

Real-World Use Case: Customer Age Verification

In a real-world scenario, you might use the containsValue method to verify if a particular age is present in a customer database before offering age-specific discounts or promotions.

Example

import java.util.HashMap;

public class CustomerAgeVerification {
    public static void main(String[] args) {
        // Creating a HashMap with String keys (customer names) and Integer values (ages)
        HashMap<String, Integer> customerDatabase = new HashMap<>();

        // Adding entries to the HashMap
        customerDatabase.put("Ravi Kumar", 25);
        customerDatabase.put("Priya Sharma", 30);
        customerDatabase.put("Vijay Singh", 35);

        // Age to be searched
        int ageToSearch = 30;

        // Checking if the age exists in the database
        if (customerDatabase.containsValue(ageToSearch)) {
            System.out.println("Age " + ageToSearch + " is present in the customer database.");
        } else {
            System.out.println("Age " + ageToSearch + " is not present in the customer database.");
        }
    }
}

Output:

Age 30 is present in the customer 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 offering promotions or verifying customer details.

Comments