java.util.HashMap.size() Method



Description

The size() method is used to return the number of key-value mappings in this map.

Declaration

Following is the declaration for java.util.HashMap.size() method.

public int size()

Parameters

NA

Return Value

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

Exception

NA

Example

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
java_util_hashmap.htm
Advertisements