Java HashMap size()

The syntax of the size() method is:

hashmap.size()

Here, hashmap is an object of the HashMap class.


size() Parameters

The size() method does not take any parameters.


size() Return Value

  • returns the number of key/value mappings present in the hashmap

Example: Java HashMap size()

Output

HashMap: {Canada=Ottawa, USA=Washington, UK=London}
Size of HashMap: 3

In the above example, we have created a hashmap named countries. Here, we have used the size() method to get the number of key/value mappings present in the hashmap.