Java HashMap containsKey() Method

The HashMap.containsKey() method in Java is used to check if a specific key 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. 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 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;

public class ContainsKeyExample {
    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 keys in the HashMap
        boolean containsRavi = people.containsKey("Ravi");
        boolean containsAmit = people.containsKey("Amit");

        // Printing the results
        System.out.println("HashMap contains key 'Ravi': " + containsRavi);
        System.out.println("HashMap contains key 'Amit': " + containsAmit);
    }
}

Output:

HashMap contains key 'Ravi': true
HashMap contains key 'Amit': 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;

public class EmployeeIDVerification {
    public static void main(String[] args) {
        // Creating a HashMap with String keys (employee IDs) and String values (employee names)
        HashMap<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.

Comments