Java Collection How to - Convert HashSet to Enumeration








Question

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

Answer

 /*from  w  w w  .  j a  va 2s.  c o m*/


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.