VOOZH about

URL: https://www.geeksforgeeks.org/java/hashmap-size-method-in-java/

⇱ Java HashMap size() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java HashMap size() Method

Last Updated : 11 Jul, 2025

The size() method of the Java HashMap class is used to retrieve the number of key-value pairs currently stored in the HashMap.

Example 1: This example demonstrates the use of the size() method to get the number of elements in the HashMap.


Output
Size of the HashMap: 3

Syntax of HashMap size() Method

public int size()

Return Type: It returns an int value representing the number of key-value mapping in the HashMap.

Example 2: This example demonstrates the size of the HashMap after removing an element.


Output
Size of the HashMap: 4
Size of the HashMap After removal: 3
Comment