Home Tutorial Java Core HashMap Example of size() method of HashMap.

 
 

Example of size() method of HashMap.
Posted on: April 6, 2011 at 12:00 AM
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

Related Tags for Example of size() method of HashMap.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.