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

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *