Convert Iterator to an Enumeration in Java
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:
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 ↯Tweet Follow @pfreitag
Convert Iterator to an Enumeration in Java was first published on October 10, 2015.