Java Utility Library

Java Dictionary - elements() Method



The java.util.Dictionary.elements() method returns an enumeration of the values in this dictionary. The returned Enumeration will generate all the elements contained in entries in this dictionary.

Syntax

public abstract Enumeration<V> elements()

Here, V is the type of value maintained by the container.


Parameters

No parameter is required.

Return Value

Returns an enumeration of the values in this dictionary.

Exception

NA.

Example:

In the example below, the java.util.Dictionary.elements() method returns an enumeration of the values in the given dictionary.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a dictionary
    Dictionary<Integer, String> Dict = 
        new Hashtable<Integer, String>();

    //populating dictionary
    Dict.put(101, "John");
    Dict.put(102, "Marry");
    Dict.put(103, "Kim");
    Dict.put(104, "Sam");
    Dict.put(105, "Jo");

    //printing the content of the dictionary
    System.out.println("Dict contains: " + Dict);

    //creating an Enum of values of the dictionary
    Enumeration MyEnum = Dict.elements();

    //printing the Enum info
    System.out.println("MyEnum is: " + MyEnum);

    //printing the content of the Enum
    System.out.print("MyEnum contains: ");
    while (MyEnum.hasMoreElements()) {
      System.out.print(MyEnum.nextElement() + " ");
    }
  }
}

The output of the above code will be:

Dict contains: {105=Jo, 104=Sam, 103=Kim, 102=Marry, 101=John}
MyEnum is: java.util.Hashtable$Enumerator@2f2c9b19
MyEnum contains: Jo Sam Kim Marry John 

❮ Java.util - Dictionary