Introduction
The Collection interface defines an iterator method that returns an object that implements the Iterator interface, which is shown in the class diagram below.
<<Interface>> Iterator<E> |
hasNext():boolean
|
An Iterator object serves two functions: (a) a way to iterate over all the objects in a collection without having to know how they are stored in the collection. (b) a way to remove items from a collection while you are iterating over it. Any data structure that implements an Iterator can be manipulated using the same interface. Iterators are often more efficient than directly accessing elements as well.