Example of size() method of HashMap.


 

Example of size() method of HashMap.

In this tutorial you will learn about the size() method of HashMap class. It return the int value and is used to get the size of the HashMap object.

In this tutorial you will learn about the size() method of HashMap class. It return the int value and is used to get the size of the HashMap object.

Example of size() method of HashMap.

A HashMap is class of collection framwork. It stores data in the form of name value pair. It provides number of methods. The size()

is also a method of HashMap class. It returns the size of HashMap, size means total number of keys in HashMap.  

Code

HashMapValues.java

package net.roseindia.java;

import java.util.HashMap;

public class HashMapSizeExample {

public static void main(String[] arr) {

/* Create a object of HfasHMap */

HashMap<Integer, String> obMap = new HashMap<Integer, String>();

/* Add values into HashMap */

obMap.put(new Integer(1), "AAAA");

obMap.put(new Integer(2), "BBBB");

obMap.put(new Integer(3), "CCCC");

obMap.put(new Integer(4), "DDDD");

obMap.put(new Integer(5), "EE");

/* Calculate the size of HashMap */

System.out.println("Size of HashMap :" + obMap.size()); } }

Output :

Size of HashMap :5

Ads