Treat Enumeration<T> as Iterator<T>

fredoverflow picture fredoverflow · Feb 15, 2011 · Viewed 8.9k times · Source

I have a class that implements the Enumeration<T> interface, but Java's foreach loop requires the Iterator<T> interface. Is there an Enumeration to Iterator Adapter in Java's standard library?

Answer

David picture David · Sep 26, 2013

If you just want something to iterate over in a for-each loop (so an Iterable and not only an Iterator), there's always java.util.Collections.list(Enumeration<T> e) (without using any external libraries).