Java HashMap containsKey() Method

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

Table of Contents

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

Introduction

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

containsKey() Method Syntax

The syntax for the containsKey method is as follows:

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

Examples

Checking for the Presence of Keys in a HashMap

The containsKey method can be used to check if a specific key is present in a HashMap.

Example

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

public class ContainsKeyExample {
    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 keys in the HashMap
        boolean containsIndia = countries.containsKey("India");
        boolean containsBrazil = countries.containsKey("Brazil");

        // Printing the results
        System.out.println("HashMap contains key 'India': " + containsIndia);
        System.out.println("HashMap contains key 'Brazil': " + containsBrazil);
    }
}

Output:

HashMap contains key 'India': true
HashMap contains key 'Brazil': false

Real-World Use Case: Verifying Employee IDs

In a real-world scenario, you might use the containsKey method to verify if an employee ID 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 EmployeeIDVerification {
    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 ID to be searched
        String employeeIdToSearch = "E002";

        // Checking if the employee ID exists in the database
        if (employeeDatabase.containsKey(employeeIdToSearch)) {
            System.out.println("Employee ID " + employeeIdToSearch + " exists. Employee Name: " + employeeDatabase.get(employeeIdToSearch));
        } else {
            System.out.println("Employee ID " + employeeIdToSearch + " does not exist.");
        }
    }
}

Output:

Employee ID E002 exists. Employee Name: Priya Sharma

Conclusion

The HashMap.containsKey() method in Java provides a way to check if a specific key is present in a HashMap. By understanding how to use this method, you can efficiently verify the presence of keys and make decisions based on their existence. This method is useful when you need to ensure that a key 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