Display HashTable content using enumeration

Q. Write a program to use enumeration to display content of HashTable.

Answer:

Below example of HashTable shows how to get all values as Enumeration object. The put() method is used to add the elements in the HashTable. By using Enumeration methods like hasMoreElements() and nextElement() we can read all values from HashTable

HashTable.java

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

class HashTable
{
        public static void main(String[] args)
        {
                Hashtable<String, String> hashtabel = new Hashtable<String, String>();
                hashtabel.put("1","First");
                hashtabel.put("2","Second");
                hashtabel.put("3","Third");
                hashtabel.put("4","Forth");
                hashtabel.put("5","Fifth");
                System.out.println("Size of Hashtable: "+hashtabel.size());
                System.out.println("Hashtable in empty: "+hashtabel.isEmpty());
                System.out.println("Elements of the Hashtable:");
                Enumeration value  = hashtabel.elements();
                while (value.hasMoreElements())
                {
                        System.out.println(value.nextElement());
                }
        }
}


Output:

hash table