1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* Iterate through keys of Java Hashtable example This Java Example shows how to iterate through the keys contained in the Hashtable object. */ import java.util.Hashtable; import java.util.Enumeration; public class IterateThroughKeysOfHashtableExample { public static void main(String[] args) { //create Hashtable object Hashtable ht = new Hashtable(); //add key value pairs to Hashtable ht.put("1","One"); ht.put("2","Two"); ht.put("3","Three"); /* get Enumeration of keys contained in Hashtable using Enumeration keys() method of Hashtable class */ Enumeration e = ht.keys(); //iterate through Hashtable keys Enumeration while(e.hasMoreElements()) System.out.println(e.nextElement()); } } /* Output would be 3 2 1 */ |
Add Comment