- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to use enumeration to display contents of HashTable in Java
Problem Description
How to use enumeration to display contents of HashTable?
Solution
Following example uses hasMoreElements & nestElement Methods of Enumeration Class to display the contents of the HashTable.
import java.util.Enumeration; import java.util.Hashtable; public class Main { public static void main(String[] args) { Hashtable ht = new Hashtable(); ht.put("1", "One"); ht.put("2", "Two"); ht.put("3", "Three"); Enumeration e = ht.elements(); while(e.hasMoreElements()) { System.out.println(e.nextElement()); } } }
Result
The above code sample will produce the following result.
Three Two One
java_collections.htm
Advertisements