Why is Java's Iterator not an Iterable? Why is Java's Iterator not an Iterable? java java

Why is Java's Iterator not an Iterable?


An iterator is stateful. The idea is that if you call Iterable.iterator() twice you'll get independent iterators - for most iterables, anyway. That clearly wouldn't be the case in your scenario.

For example, I can usually write:

public void iterateOver(Iterable<String> strings){    for (String x : strings)    {         System.out.println(x);    }    for (String x : strings)    {         System.out.println(x);    }}

That should print the collection twice - but with your scheme the second loop would always terminate instantly.


Because an iterator generally points to a single instance in a collection. Iterable implies that one may obtain an iterator from an object to traverse over its elements - and there's no need to iterate over a single instance, which is what an iterator represents.


For my $0.02, I completely agree that Iterator should not implement Iterable, but I think the enhanced for loop should accept either. I think the whole "make iterators iterable" argument comes up as a work around to a defect in the language.

The whole reason for the introduction of the enhanced for loop was that it "eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays" [1].

Collection<Item> items...for (Iterator<Item> iter = items.iterator(); iter.hasNext(); ) {    Item item = iter.next();    ...}for (Item item : items) {    ...}

Why then does this same argument not hold for iterators?

Iterator<Iter> iter.....while (iter.hasNext()) {    Item item = iter.next();    ...}for (Item item : iter) {    ...}

In both cases, the calls to hasNext() and next() have been removed, and there is no reference to the iterator in the inner loop. Yes, I understand that Iterables can be re-used to create multiple iterators, but that all happens outside of the for loop: inside the loop there is only ever a forward progression one item at a time over the items returned by the iterator.

Also, allowing this would also make it easy to use the for loop for Enumerations, which, as has been pointed out elsewhere, are analogous to Iterators not Iterables.

So don't make Iterator implement Iterable, but update the for loop to accept either.

Cheers,