Here is my understanding on significance of using Iterable
and Iterator
in pre 1.8 java.
1)
java.util.AbstractList
is Iterable
because it implements,
Iterator<T> iterator();
, which is a contract for any class to be Iterable
.
2)
java.util.AbstractList
implements,
Iterator<T> iterator();
by creating an instance level inner class,
private class Itr implements Iterator<E> { ... }
that implements hasNext
, next
and remove
methods.
private class ListItr extends Itr implements ListIterator<E>{..}
is just an extra facility over above iterator for List
type implementations.
3)
Is this the only purpose of these two interfaces Iterable
& Iterator
, to enable any object to be iterable? Please let me know, if there is any other purpose of these two interfaces?