Java HashMap size() Method

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The HashMap.size() method in Java is used to get the number of key-value mappings 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. size Method Syntax
  3. Examples
    • Getting the Size of a HashMap
    • Real-World Use Case: Inventory Count
  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 mappings currently present in the HashMap. This can be useful when you need to know the number of entries in the map for various operations, such as iteration or validation.

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 integer 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 get the number of key-value mappings in a HashMap.

Example

import java.util.HashMap; public class SizeExample { 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); // Getting the size of the HashMap int size = people.size(); // Printing the size of the HashMap System.out.println("Size of HashMap: " + size); } } 

Output:

Size of HashMap: 3 

Real-World Use Case: Inventory Count

In a real-world scenario, you might use the size method to get the count of items in an inventory system.

Example

import java.util.HashMap; public class InventoryCount { public static void main(String[] args) { // Creating a HashMap with String keys (product names) and Integer values (quantities) HashMap<String, Integer> inventory = new HashMap<>(); // Adding products to the inventory inventory.put("Laptops", 10); inventory.put("Smartphones", 15); inventory.put("Tablets", 5); // Getting the size of the inventory int inventorySize = inventory.size(); // Printing the size of the inventory System.out.println("Number of products in inventory: " + inventorySize); } } 

Output:

Number of products in inventory: 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 the contents of a HashMap and perform operations that depend on the number of entries. This method is useful in various scenarios, such as tracking the number of items in an inventory or validating the size of a data structure.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare