Share on Google+Share on Google+

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.

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.  



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



Posted on: April 6, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.