Home > Java > javaTutorial > How to display elements of a Hashtable in Java using enumeration?

How to display elements of a Hashtable in Java using enumeration?

王林
Release: 2023-09-16 12:41:02
forward
1120 people have browsed it

How to display elements of a Hashtable in Java using enumeration?

A Hashtable is a powerful data structure in Java that allows programmers to store and organize data in the form of key-value pairs. Many applications require retrieving and displaying entries from a Hashtable.

In a Hashtable, any non-empty object can be used as a key or value. However, in order to successfully store and retrieve items in a Hashtable, the object used as a key must implement the equals() method and the hashCode() method. These implementations ensure the correctness of key comparisons and hashing, enabling efficient management and retrieval of data in Hashtables.

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

By using enumeration methods like hasMoreElements() and nextElement() we can efficiently retrieve all keys and values ​​associated with a Hashtable and get them as enumerations Object acquisition. This approach allows seamless traversal and extraction of data in the Hashtable

Advantages of using enumerations:

  • Efficiency: When using old collection classes (such as Hashtable), enumeration is lightweight and efficient because it does not rely on iterators.

  • Thread Safety: Enumeration is a read-only interface, unlike iterators, which makes it thread-safe and a good choice in multi-threaded environments

Now let us use some examples to illustrate how to use Enumeration to get elements from Hashtable.

The Chinese translation of

Example 1

is:

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());
      }
   }
}
Copy after login

Output

Employee Names
==============
Hari
Vamsi
Rohith
Copy after login

In the previous example, we just displayed the employee's name, now we will display the employee's ID and 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));
      }
   }
}
Copy after login

Output

EmpId   EmpName
================
 87    Hari
 84    Vamsi
 72    Rohith
Copy after login

in conclusion

In this article, we have discussed the concepts of Hashtable and Enumeration and their advantages, and we have also seen several examples of how to use this Enumeration to get elements from a Hashtable.

The above is the detailed content of How to display elements of a Hashtable in Java using enumeration?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template