Pete Freitag Pete Freitag

Convert Iterator to an Enumeration in Java

Updated on June 04, 2020
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 Interator into an Enumeration with java.

java apache

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

Weekly Security Advisories Email

Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).