Iterable<E> is in java.lang whereas Iterator<E> is in java.util. Is there a good reason for this or is this merely an artifact of bad design?
It seems strange since the only thing that an Iterable<E> is good for is providing an Iterator<E>.
EDIT: One potential reason is because of the (then-)newly introduced for-each loop. I guess my question then would be, are they equivalent?
for(Object o : collection)
...
vs
for( Iterator iter = collection.iterator(); iter.hasNext(); ) {
o = iter.next();
...
If they are, then that still doesn’t explain why the two classes are in different packages since the compiler would have to import java.util anyways to use the Iterator construct.
Part of it is history:
Iteratorhas been with us since JDK 1.2, andIterablecame with JDK 1.5.Iterablecame in with the enhancedforloop.Bad design? No, evolution. There’s no all-knowing creator. As lessons are learned they’re incorporated into the JDK.