Pete Freitag Pete Freitag

Convert Iterator to an Enumeration in Java

Updated: June 04, 2020

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 Interator into an Enumeration with java.

Like this? Follow me ↯

Convert Iterator to an Enumeration in Java was first published on October 10, 2015.