Java HashMap remove() Method

The HashMap.remove(Object key) method in Java is used to remove the mapping for a specified key from 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. remove Method Syntax
  3. Examples
    • Removing a Mapping from a HashMap
    • Real-World Use Case: Employee Termination
  4. Conclusion

Introduction

The remove(Object key) method is a member of the HashMap class in Java. It allows you to remove the mapping for a specified key if it is present in the HashMap. This can be useful when you need to delete an entry from the map based on its key.

remove() Method Syntax

The syntax for the remove method is as follows:

public V remove(Object key)
  • The method takes a single parameter key of type Object, which represents the key whose mapping is to be removed from the HashMap.
  • The method returns the value associated with the specified key, or null if the map contained no mapping for the key.

Examples

Removing a Mapping from a HashMap

The remove method can be used to remove a mapping from a HashMap.

Example

import java.util.HashMap;

public class RemoveExample {
    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);

        // Removing an entry from the HashMap
        Integer removedValue = people.remove("Priya");

        // Printing the removed value and the resulting HashMap
        System.out.println("Removed value: " + removedValue);
        System.out.println("Updated HashMap: " + people);
    }
}

Output:

Removed value: 30
Updated HashMap: {Ravi=25, Vijay=35}

Real-World Use Case: Employee Termination

In a real-world scenario, you might use the remove method to delete an employee's details from the database upon their termination.

Example

import java.util.HashMap;

public class EmployeeTermination {
    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 removed
        String employeeIdToRemove = "E002";

        // Removing the employee details from the database
        String removedEmployee = employeeDatabase.remove(employeeIdToRemove);

        // Checking if the employee ID was found and removed
        if (removedEmployee != null) {
            System.out.println("Removed Employee: " + removedEmployee);
        } else {
            System.out.println("Employee ID " + employeeIdToRemove + " does not exist in the database.");
        }

        // Printing the updated employee database
        System.out.println("Updated Employee Database: " + employeeDatabase);
    }
}

Output:

Removed Employee: Priya Sharma
Updated Employee Database: {E001=Ravi Kumar, E003=Vijay Singh}

Conclusion

The HashMap.remove(Object key) method in Java provides a way to remove the mapping for a specified key from a HashMap. By understanding how to use this method, you can efficiently manage the contents of a HashMap by deleting entries as needed. This method is useful in various scenarios, such as removing outdated information or handling data deletions in applications.

Comments