Java HashMap size() Method Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report 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. Java // Java program to demonstrates the working of size() import java.util.HashMap; public class Geeks { public static void main(String[] args) { // Creating a HashMap HashMap<String, Integer> hm = new HashMap<>(); // Adding elements to the HashMap hm.put("Java", 10); hm.put("C++", 20); hm.put("python", 30); System.out.println("Size of the HashMap: " + hm.size()); } } OutputSize 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. Java // Java program to demomnstrates the use // of size() to track the number of entries // in the HashMap import java.util.HashMap; public class Geeks { public static void main(String[] args) { // Creating a HashMap HashMap<String, Integer> hm = new HashMap<>(); // Adding elements to the HashMap hm.put("Java", 10); hm.put("C++", 20); hm.put("Python", 30); hm.put("JavaScript", 40); System.out.println("Size of the HashMap: " + hm.size()); // Removing an element hm.remove("Python"); // Displaying the size of the // HashMap after removal System.out.println( "Size of the HashMap After removal: " + hm.size()); } } OutputSize of the HashMap: 4 Size of the HashMap After removal: 3 Comment C chinmoy lenka Follow 2 Improve C chinmoy lenka Follow 2 Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-HashMap +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like