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


A Hashtable is a powerful data structure in Java that allows programmers to store and organise data in key-value pairs. Many applications need retrieving and displaying entries from a Hashtable.

In a Hashtable, any non-null object can serve as a key or a value. However, for successful storage and retrieval of items from the Hashtable, the objects used as keys must implement both the equals() method and the hashCode() method. These implementations ensure proper handling of key comparisons and hashing, enabling efficient management and retrieval of data within the Hashtable.

Through the utilization of the keys() and elements() methods in the Hashtable, we gain access to Enumeration objects containing the keys and values.

By employing enumeration methods like hasMoreElements() and nextElement(), we can effectively retrieve all the keys and values linked to the Hashtable, obtaining them as enumeration objects. This approach allows for seamless traversal and extraction of the data within the Hashtable.

Advantages of Using Enumeration:

  • Efficiency: When working with legacy collection classes like Hashtable, enumeration is lightweight and efficient since it doesn't rely on iterators.

  • Thread-Safety: Enumeration is a read-only interface unlike Iterator, which makes it thread-safe and a good choice for multi-threaded situations.

Now let’s go through few examples on how we can use Enumeration to get the elements from the Hashtable

Example 1

import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;


public class App {
   public static void main(String[] args)
   {
      // we will firstly create a empty hashtable
      Hashtable<Integer, String> empInfo
         = new Hashtable<Integer, String>();

      // now we will insert employees data into the hashtable
      //where empId would be acting as key and name will be the value
      empInfo.put(87, "Hari");
      empInfo.put(84, "Vamsi");
      empInfo.put(72, "Rohith");

      // now let's create enumeration object
      //to get the elements which means employee names
      Enumeration<String> empNames = empInfo.elements();

      System.out.println("Employee Names");
      System.out.println("==============");
      // now we will print all the employee names using hasMoreElements() method
      while (empNames.hasMoreElements()) {
         System.out.println(empNames.nextElement());
      }
   }
}

Output

Employee Names
==============
Hari
Vamsi
Rohith

In the previous example we have just displayed the names of employees now we will display the ID along with the employee name.

Example 2

import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;


public class App {
   public static void main(String[] args)
   {
      // we will firstly create a empty hashtable
      Hashtable<Integer, String> empInfo
         = new Hashtable<Integer, String>();

      // now we will insert employees data into the hashtable
      //where empId would be acting as key and name will be the value
      empInfo.put(87, "Hari");
      empInfo.put(84, "Vamsi");
      empInfo.put(72, "Rohith");

      // now let's create enumeration objects
      // to store the keys
      
      Enumeration<Integer> empIDs = empInfo.keys();

      System.out.println("EmpId" + "  \t"+ "EmpName");
      System.out.println("================");
      // now we will print all the employee details
      // where key is empId and with the help of get() we will get corresponding
      // value which will be empName
      while (empIDs.hasMoreElements()) {
         int key = empIDs.nextElement();
         System.out.println( " "+ key + "  \t" + empInfo.get(key));
      }
   }
}

Output

EmpId   EmpName
================
 87    Hari
 84    Vamsi
 72    Rohith

Conclusion

In this article we have discussed about the Hashtable and concept of Enumeration and its advantages and we have also seen how to use this Enumeration to get the elements from the Hashtable with few examples.

Updated on: 03-Aug-2023

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements