Convert Iterator to an Enumeration in Java

Updated , First Published by Pete Freitag

Sometimes a Java API requires that you pass an Enumeration, of values, but your object only supplies an iterator method. In most cases, if the object is a Collection, you can simply use the java.util.Collections static method: enumeration(Collection c).

Here's an example:

Enumeration e = java.util.Collections.enumeration(mySet);

There is also the Apache Commons Collections IteratorUtils which has an asEnumeration method which accepts an Iterator and returns a Enumeration. I prefer using the Native Java API rather than adding another library for such a simple task, however if you already use Commons that that might also be a simple way to convert an Iterator into an Enumeration with java.