Easy Tutorial
❮ Java Datainputstream Java Arraylist Trimtosize ❯

Java Example - Traversing HashTable Keys and Values

Java Examples

The following example demonstrates how to use the keys() method of the Hashtable class to traverse and output keys and values:

Main.java File

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.keys();
      while (e.hasMoreElements()){
         System.out.println(e.nextElement());
      }
   }
}

The above code outputs the following results when run:

3
2
1

Java Examples

❮ Java Datainputstream Java Arraylist Trimtosize ❯