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?

- 256,549
- 94
- 388
- 662
-
Its a lot simpler to either, not use Enumerator as its a legacy class or not use the "enhanced" for each loop. – Peter Lawrey Feb 15 '11 at 17:40
-
5The for-each loop requires an Iterable, not an Iterator; which do you really want? – Michael Myers Apr 10 '12 at 21:22
7 Answers
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).

- 1,359
- 1
- 13
- 20
-
2This is a simple answer, using a standard java class/method specifically designed for this purpose. – aeropapa17 May 02 '16 at 17:14
You need a so called "Adapter", to adapt the Enumeration
to the otherwise incompatible Iterator
. Apache commons-collections has EnumerationIterator
. The usage is:
Iterator iterator = new EnumerationIterator(enumeration);

- 455
- 1
- 6
- 19

- 588,226
- 146
- 1,060
- 1,140
-
1this is answering the question, because you can't use an iterator in a foreach loop. – Edward Q. Bridges Jan 18 '13 at 14:37
a) I'm pretty sure you mean Enumeration
, not Enumerator
b) Guava provides a Helper method Iterators.forEnumeration(enumeration)
that generates an iterator from an Enumeration, but that won't help you either, as you need an Iterable
(a provider of Iterators), not an Iterator
c) you could do it with this helper class:
public class WrappingIterable<E> implements Iterable<E>{
private Iterator<E> iterator;
public WrappingIterable(Iterator<E> iterator){
this.iterator = iterator;
}
@Override
public Iterator<E> iterator(){
return iterator;
}
}
And now your client code would look like this:
for(String string : new WrappingIterable<String>(
Iterators.forEnumeration(myEnumeration))){
// your code here
}
But is that worth the effort?

- 292,901
- 67
- 465
- 588
-
You could let your wrapper constructor accept an enumeration. – Thorbjørn Ravn Andersen Apr 10 '12 at 19:07
-
3Also note that `Iterable` means you can ask for the iterator repeatedly. This wrapper does not comply with that. – Thorbjørn Ravn Andersen Apr 10 '12 at 23:28
No need to roll your own. Look at Google's Guava library. Specifically
Iterators.forEnumeration()

- 8,124
- 29
- 28
-
1
-
@ThorbjørnRavnAndersen - The OP explicitly asked for an adapter from Enumeration to Iterator. He did not ask for Enumeration to Iterable. – rfeak Apr 10 '12 at 14:53
-
2He wants to use it in `foreach`, hence the `Iterator` is a typo. – Thorbjørn Ravn Andersen Apr 10 '12 at 16:23
There's nothing that is part of the standard library. Unfortunately you'll have to roll your own adapter. There are examples out there of what others have done, for example:

- 2,446
- 19
- 33
-
1Yes, there is something in the standard library specifically for this purpose: Collections.list – aeropapa17 May 02 '16 at 17:12
or in commons-collections EnumerationUtils
import static org.apache.commons.collections.EnumerationUtils.toList
toList(myEnumeration)

- 11
- 1
If you can modify the class then you can simply implement Iterator<T>
too and add the remove
method..

- 131,802
- 30
- 241
- 343