The size() method is used to return the number of key-value mappings in this map.
Following is the declaration for java.util.HashMap.size() method.
public int size()
NA
The method call returns the number of key-value mappings in this map.
NA
The following example shows the usage of java.util.HashMap.size()
package com.tutorialspoint; import java.util.*; public class HashMapDemo { public static void main(String args[]) { // create hash map HashMap newmap = new HashMap(); // populate hash map newmap.put(1, "tutorials"); newmap.put(2, "point"); newmap.put(3, "is best"); System.out.println("Size of the map: "+ newmap.size()); } }
Let us compile and run the above program, this will produce the following result.
Size of the map: 3