Get Size of Java HashMap Example

Get Size of Java HashMap Example


Posted in : Java Posted on : December 2, 2010 at 5:56 PM Comments : [ 0 ]

This Java Example shows how to get the Number of elements in the HashMap object i.e size of HashMap object in java.

Get Size of Java HashMap Example

This Java Example shows how to get the Number of elements in the HashMap object i.e size of HashMap object in java. To get no. of element in the HashMap object we use int size() method. This method returns number of the elements in the specified HashMap object.

 Example: SizeOfHashMap.java

package devmanuals.com;

import java.util.*;

public class SizeOfHashMap {
	public static void main(String args[]) {
		HashMap hm = new HashMap();
		hm.put(1, "Gyan");
		hm.put(2, "Ankit");
		hm.put(3, "Arun");
		hm.put(4, "Anand");
		hm.put(5, "Ram");

		System.out.println("The size of HashMap is : " + hm.size());
		Object obj = hm.remove(3);
		System.out.println("The value of removed key is : " + obj);
		System.out.println("The size of HashMap after alteration is : "
				+ hm.size());

	}
}

Output:

The size of HashMap is : 5 

The value of removed key is : Arun 

The size of HashMap after alteration is : 4

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics