Java HashMap isEmpty() Method

The HashMap.isEmpty() method in Java is used to check if a HashMap contains no key-value mappings. 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. isEmpty Method Syntax
  3. Examples
    • Checking if a HashMap is Empty
    • Real-World Use Case: Inventory Check
  4. Conclusion

Introduction

The isEmpty() method is a member of the HashMap class in Java. It allows you to check if the HashMap is empty, meaning it contains no key-value mappings. This can be useful when you need to determine if the HashMap has been populated with data or if it is still empty.

isEmpty() Method Syntax

The syntax for the isEmpty method is as follows:

public boolean isEmpty()
  • The method does not take any parameters.
  • The method returns a boolean value:
    • true if the HashMap contains no key-value mappings.
    • false if the HashMap contains one or more key-value mappings.

Examples

Checking if a HashMap is Empty

The isEmpty method can be used to check if a HashMap is empty.

Example

import java.util.HashMap;

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

        // Checking if the HashMap is empty
        boolean isEmptyBefore = people.isEmpty();

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

        // Checking if the HashMap is empty after adding entries
        boolean isEmptyAfter = people.isEmpty();

        // Printing the results
        System.out.println("Is HashMap empty before adding entries? " + isEmptyBefore);
        System.out.println("Is HashMap empty after adding entries? " + isEmptyAfter);
    }
}

Output:

Is HashMap empty before adding entries? true
Is HashMap empty after adding entries? false

Real-World Use Case: Inventory Check

In a real-world scenario, you might use the isEmpty method to check if an inventory is empty before performing operations such as restocking or processing orders.

Example

import java.util.HashMap;

public class InventoryCheck {
    public static void main(String[] args) {
        // Creating a HashMap with String keys (product names) and Integer values (quantities)
        HashMap<String, Integer> inventory = new HashMap<>();

        // Checking if the inventory is empty
        if (inventory.isEmpty()) {
            System.out.println("Inventory is empty. Need to restock.");
        } else {
            System.out.println("Inventory has items.");
        }

        // Adding products to the inventory
        inventory.put("Laptops", 10);
        inventory.put("Smartphones", 15);

        // Checking if the inventory is empty after adding items
        if (inventory.isEmpty()) {
            System.out.println("Inventory is empty. Need to restock.");
        } else {
            System.out.println("Inventory has items.");
        }
    }
}

Output:

Inventory is empty. Need to restock.
Inventory has items.

Conclusion

The HashMap.isEmpty() method in Java provides a way to check if a HashMap contains no key-value mappings. By understanding how to use this method, you can efficiently determine if a HashMap has been populated with data. This method is useful in various scenarios, such as checking if an inventory is empty or verifying if a data structure has been initialized with data.

Comments