Java Collection How to - Convert Set to Enumeration








Question

We would like to know how to convert Set to Enumeration.

Answer

//  w w  w.jav a2s .  com
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;

public class Main {
  public static void main(String[] args) {
    HashSet<String> hashSet = new HashSet<String>();

    hashSet.add("A");
    hashSet.add("B");
    hashSet.add("D");
    hashSet.add("E");
    hashSet.add("F");

    Enumeration e = Collections.enumeration(hashSet);
    while (e.hasMoreElements()){
      System.out.println(e.nextElement());
    }
  }
}

The code above generates the following result.