java.util.HashMap size()

Description :

On this document we will be showing a java example on how to use the size() method. Basically this method just return how many key-value combination in our HashMap. This method might be useful in situation wherein we have to determine the size of our HashMap before performing business operations. Important notes for size method:

  • specified by size in interface Map<K,V>
  • overrides size in class AbstractMap<K,V>

Method Syntax :

public int size()

Parameter Input :


DataType Parameter Description


Method Returns :

The size method returns the number of key-value mappings in this map.

Compatibility Version :

Requires Java 1.2 and up

Exception :


Java Code Example :

This java example source code demonstrates the use of size() method of HashMap class.

Sample Output :

Running the above java example of the usage of HashMap method will give the following output:

HashHamp size method example

HashHamp size method example output

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :