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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/* Get Set view of Keys from Java Hashtable example This Java Example shows how to get a Set of keys contained in Hashtable using keySet method of Java Hashtable class. */ import java.util.Enumeration; import java.util.Iterator; import java.util.Hashtable; import java.util.Set; public class GetSetViewOfKeysFromHashtableExample { 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 Set of keys contained in Hashtable using Set keySet() method of Hashtable class */ Set st = ht.keySet(); System.out.println("Set created from Hashtable Keys contains :"); //iterate through the Set of keys Iterator itr = st.iterator(); while(itr.hasNext()) System.out.println(itr.next()); /* Please note that resultant Set object is backed by the Hashtable. Any key that is removed from Set will also be removed from original Hashtable object. The same is not the case with the element addition. */ //remove 2 from Set st.remove("2"); //print keys of original Hashtable System.out.println("Hashtable keys after removal from Set are :"); Enumeration e = ht.keys(); while(e.hasMoreElements()) System.out.println(e.nextElement()); } } /* Output would be Set created from Hashtable Keys contains : 3 2 1 Hashtable keys after removal from Set are : 3 1 */ |
Add Comment