How to Use Enumeration to Display Elements of Hashtable in Java?

Prerequisite: Hashtable in Java

We can get the keys and values of a Hashtable as an Enumeration object. Using keys() and elements() method, we can obtain all keys and values respectively as an Enumeration object. Using Enumeration methods like hasMoreElements() and nextElement() we can read all keys and values corresponding to from a Hashtable.

Example 1:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getting values as an Enumeration
  
import java.util.Enumeration;
import java.util.Hashtable;
import java.io.*;
  
public class EnumerationOnKeys {
    public static void main(String[] args)
    {
        // initialize an object of Hashtable
        Hashtable<Integer, String> ht
            = new Hashtable<Integer, String>();
        
        // insert key-value pairs into hash table
        ht.put(1, "Geeks");
        ht.put(2, "for");
        ht.put(3, "Geeks");
        
        // create an Enumeration object to read elements
        Enumeration e = ht.elements();
  
        // print elements of hashtable using enumeration
        while (e.hasMoreElements()) {
                
              // print the current element
            System.out.println(e.nextElement());
        }
    }
}

chevron_right


Output

Geeks
for
Geeks

Example 2:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getting keys as an Enumeration
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // create hashtable
        Hashtable<String, String> ht
            = new Hashtable<String, String>();
  
        // put key-value pairs into hashtable
        ht.put("Name", "Rohan");
        ht.put("Age", "23");
        ht.put("Address", "India");
        ht.put("Article", "GeeksforGeeks");
  
        // create enumeration to store keys
        Enumeration<String> e = ht.keys();
  
        // while keys are present
        while (e.hasMoreElements()) {
  
            // get key
            String key = e.nextElement();
  
            // print key and value corresponding to that key
            System.out.println(key + ":" + ht.get(key));
        }
    }
}

chevron_right


Output

Name:Rohan
Article:GeeksforGeeks
Age:23
Address:India

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.