Publish Your Article Here

Program: How to read elements using Enumeration in Hashtable?


Below example shows how to get all keys as Enumeration object. By calling keys() method, we can get all keys as Enumeration object. By using Enumeration methods like hasMoreElements() and nextElement() we can read all values from Hashtable.

package com.java2novice.hashtable;

import java.util.Enumeration;
import java.util.Hashtable;

public class MyHashtableEnumaration {

public static void main(String a[]){
                Hashtable<String, String> hm = new Hashtable<String, String>();
                //add key-value pair to Hashtable
                hm.put("first", "FIRST INSERTED");
                hm.put("second", "SECOND INSERTED");
                hm.put("third","THIRD INSERTED");
                Enumeration<String> keys = hm.keys();
                        String key = keys.nextElement();
                        System.out.println("Value of "+key+" is: "+hm.get(key));

Value of third is: THIRD INSERTED
Value of second is: SECOND INSERTED
Value of first is: FIRST INSERTED
<< Previous Program | Next Program >>

List Of All Hashtable Sample Programs:

  1. Basic Hashtable Operations.
  2. How to iterate through Hashtable?
  3. How to copy Map content to another Hashtable?
  4. How to search a key in Hashtable?
  5. How to search a value in Hashtable?
  6. How to get all keys from Hashtable?
  7. How to get entry set from Hashtable?
  8. How to delete all elements from Hashtable?
  9. How to read elements using Enumeration in Hashtable?
  10. Hashtable implementation with equals and hashcode example.
  11. How to fetch values with user define object as keys in Hashtable?
  12. How to eliminate duplicate keys (user defined objects) with Hashtable?
Knowledge Centre
Java class can be private?
We can not declare top level class as private. Java allows only public and default modifier for top level classes in java. Inner classes can be private.
Famous Quotations
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2024 by Nataraja Gootooru. All Rights Reserved.