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 Collection of Values from Java Hashtable example This Java Example shows how to get a Collection of values contained in Hashtable using values method of Java Hashtable class. */ import java.util.Enumeration; import java.util.Iterator; import java.util.Hashtable; import java.util.Collection; public class GetCollectionOfValuesFromHashtableExample { 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 Collection of values contained in Hashtable using Collection values() method of Hashtable class */ Collection c = ht.values(); System.out.println("Values of Collection created from Hashtable are :"); //iterate through the collection Iterator itr = c.iterator(); while(itr.hasNext()) System.out.println(itr.next()); /* Please note that resultant Collection object is backed by the Hashtable. Any value that is removed from Collection will also be removed from original Hashtable object. The same is not the case with the element addition. */ //remove One from collection c.remove("One"); //print values of original values of Hashtable System.out.println("Hashtable values after removal from Collection are :"); Enumeration e = ht.elements(); while(e.hasMoreElements()) System.out.println(e.nextElement()); } } /* Output would be Values of Collection created from Hashtable are : Three Two One Hashtable values after removal from Collection are : Three Two */ |
Add Comment