How to Get Size of a HashMap in Java


In this example we will show the method to get the size of a HashMap in Java.

Source Code

package com.beginner.examples;

import java.util.HashMap;

public class HashMapSize {
  public static void main(String[] args) {
    HashMap h = new HashMap();
    h.put("A", "a");
    h.put("B", "b");
    h.put("C", "c");
    System.out.println(h.size()); // get the size of a HashMap
  }
}

Output:

3

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments