Get Collection of Values from Java Hashtable example

  1. /*
  2.   Get Collection of Values from Java Hashtable example
  3.   This Java Example shows how to get a Collection of values contained in Hashtable
  4.   using values method of Java Hashtable class.
  5. */
  6.  
  7. import java.util.Enumeration;
  8. import java.util.Iterator;
  9. import java.util.Hashtable;
  10. import java.util.Collection;
  11.  
  12. public class GetCollectionOfValuesFromHashtableExample {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. //create Hashtable object
  17. Hashtable ht = new Hashtable();
  18.  
  19. //add key value pairs to Hashtable
  20. ht.put("1","One");
  21. ht.put("2","Two");
  22. ht.put("3","Three");
  23.  
  24. /*
  25.   get Collection of values contained in Hashtable using
  26.   Collection values() method of Hashtable class
  27.   */
  28.  
  29. Collection c = ht.values();
  30.  
  31. System.out.println("Values of Collection created from Hashtable are :");
  32. //iterate through the collection
  33. Iterator itr = c.iterator();
  34. while(itr.hasNext())
  35. System.out.println(itr.next());
  36.  
  37. /*
  38.   Please note that resultant Collection object is backed by the Hashtable.
  39.   Any value that is removed from Collection will also be removed from
  40.   original Hashtable object. The same is not the case with the element
  41.   addition.
  42.   */
  43.  
  44. //remove One from collection
  45. c.remove("One");
  46.  
  47. //print values of original values of Hashtable
  48. System.out.println("Hashtable values after removal from Collection are :");
  49. Enumeration e = ht.elements();
  50. while(e.hasMoreElements())
  51. System.out.println(e.nextElement());
  52. }
  53. }
  54.  
  55. /*
  56. Output would be
  57. Values of Collection created from Hashtable are :
  58. Three
  59. Two
  60. One
  61. Hashtable values after removal from Collection are :
  62. Three
  63. Two
  64. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!