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

 
 

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

Advertisement

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


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 6, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com