java.util.Hashtable.keys() Method


Advertisements


Description

The keys() method is used to get an enumeration of the keys in this hashtable.

Declaration

Following is the declaration for java.util.Hashtable.keys() method.

public Enumeration<K> keys()

Parameters

NA

Return Value

The method call returns an enumeration of the keys in this hashtable.

Exception

NA

Example

The following example shows the usage of java.util.Hashtable.keys()

Live Demo
package com.tutorialspoint;

import java.util.*;

public class HashTableDemo {
   public static void main(String args[]) {
      
      // create hash table 
      Hashtable htable1 = new Hashtable();      

      // put values in table
      htable1.put(1, "A");
      htable1.put(2, "B");
      htable1.put(3, "C");
      htable1.put(4, "D");

      // create enumeration for keys
      Enumeration en = htable1.keys();

      System.out.println("Display result:"); 

      // display search result
      while (en.hasMoreElements()) {
         System.out.println(en.nextElement());
      }
   }    
}

Let us compile and run the above program, this will produce the following result.

Display result:
4
3
2
1

java_util_hashtable.htm

Advertisements