Skip to content

Java HashMap Size Example

This example shows how to get the HashMap size using the size method of the HashMap class. It is sometimes also referred to as HashMap length or number of key-value mappings stored in the HashMap.

How to get the HashMap size in Java?

The size method of the HashMap class returns the number of entries or key-value mappings stored in the HashMap.

This method returns the number of mappings stored in the map object. The return type of the size method is an int type.

Output

How to check if HashMap is empty using the size method?

You can compare the return value of the size method with 0 to check if the HashMap is empty as given below.

Output

This example is a part of the Java HashMap tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.