Java Hashtable.keys()

Syntax

Hashtable.keys() has the following syntax.

public Enumeration <K> keys()

Example

In the following code shows how to use Hashtable.keys() method.


//  w  w w . j  av  a 2s .c om


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

public class Main {
   public static void main(String args[]) {
      // create hash table 
      Hashtable<Integer,String> htable1 = new Hashtable<Integer,String>();      
      
      // put values in table
      htable1.put(1, "A");
      htable1.put(2, "B");
      htable1.put(3, "C");
      htable1.put(4, "from java2s.com");
      
      // create enumeration for keys
      Enumeration en=htable1.keys();
      
      System.out.println("Display result:"); 
    
      // display search result
      while (en.hasMoreElements()) {
         System.out.println(en.nextElement());
      }
   }    
}

The code above generates the following result.