Java HashMap size() Method

The HashMap.size() method in Java is used to return the number of key-value mappings in a HashMap.

Table of Contents

  1. Introduction
  2. size Method Syntax
  3. Examples
    • Getting the Size of a HashMap
    • Real-World Use Case: Counting Employee Records
  4. Conclusion

Introduction

The HashMap.size() method is a member of the HashMap class in Java. It allows you to determine the number of key-value pairs currently stored in the HashMap. This can be useful for operations that depend on the size of the map, such as validating data or controlling iterations.

size() Method Syntax

The syntax for the size method is as follows:

public int size()
  • The method does not take any parameters.
  • The method returns an int value representing the number of key-value mappings in the HashMap.

Examples

Getting the Size of a HashMap

The size method can be used to determine the number of entries in a HashMap.

Example

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

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

        // Getting the size of the HashMap
        int size = countries.size();

        // Printing the size of the HashMap
        System.out.println("Size of HashMap: " + size);
    }
}

Output:

Size of HashMap: 3

Real-World Use Case: Counting Employee Records

In a real-world scenario, you might use the size method to count the number of employee records in an employee database.

Example

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

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

        // Getting the size of the employee database
        int numberOfEmployees = employeeDatabase.size();

        // Printing the number of employee records
        System.out.println("Number of Employee Records: " + numberOfEmployees);
    }
}

Output:

Number of Employee Records: 3

Conclusion

The HashMap.size() method in Java provides a way to determine the number of key-value mappings in a HashMap. By understanding how to use this method, you can efficiently manage and validate the size of your HashMap in various scenarios. This method is useful for counting entries, validating data size, and controlling iterations based on the number of elements in the map.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top