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.
[ 0 ] Comments