Set view of Keys from java HashMap Example

Set view of Keys from java HashMap Example


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

In this example, We will show you how to get the set of keys from the HashMap object in java.

Set view of Keys from java HashMap Example

In this example, We will show you how to get the set of  keys from the HashMap object in java. To get a set of keys of HashMap object we use the keySet() method of HashMap Class. This method returns a Set view of the keys contained in this map.

Example:- KeySetHashMap.java

package devmanuals.com;

import java.util.*;

public class KeySetHashMap {
	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");

		Set set = hm.keySet();
		Iterator itr = set.iterator();
		System.out.println("The keys  are : ");
		while (itr.hasNext()) {
			System.out.println(itr.next());

		}

	}
}
Output:-
The keys are : 
1
2
3
4
5

Download This Code.
Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics