Java.util.Dictionary.elements() Method



Description

The java.util.Dictionary.elements() method returns an enumeration of the values in this dictionary.

Declaration

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

public abstract Enumeration<V> elements()

Parameters

NA

Return Value

This method returns an enumeration of the values in this dictionary.

Exception

NA

Example

The following example shows the usage of java.util.Dictionary.elements() 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 2 elements
      d.put(1, "Cocoa");
      d.put(4, "Chocolate" + "Bar");
      System.out.println("\"1\" is " + d.get(1));
      System.out.println("\"4\" is " + d.get(4));

      // generates a series of elements, one at a time
      for (Enumeration e = d.elements(); e.hasMoreElements();) {
         System.out.println(e.nextElement());
      }
   }
}

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

"1" is Cocoa
"4" is ChocolateBar
ChocolateBar
Cocoa
java_util_dictionary.htm
Advertisements