Java.util.Dictionary.keys() Method

Advertisements


Description

The java.util.Dictionary.keys() method gets an enumeration of the keys in this dictionary.

Declaration

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

public abstract Enumeration<K> keys()

Parameters

  • NA

Return Value

This method returns an enumeration of keys in this dictionary.

Exception

  • NA

Example

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

package com.tutorialspoint;

import java.util.*;

public class DictionaryDemo {

   public static void main(String[] args) {
      
	  // create a new hashtable
      Dictionary d = new Hashtable();
	  
      // add some elements
      d.put("1", "Chocolate");
      d.put("2", "Cocoa");
      d.put("5", "Coffee");
	  
      // return an enumeration of the keys from this dictionary.
      for (Enumeration e = d.keys(); e.hasMoreElements();) {
         System.out.println(e.nextElement());
      }
   }
}

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

5
2
1



Advertisements
Advertisements