Iterable Interface | Iterator Interface |
|---|---|
| Represents a collection that can be traversed using foreach loop. | Allows to iterate over some other collection. |
| The class that implements the iterable interface has to override iterator() method. | hasNext() and next() methods of Iterator interface are to be overridden by class implementing it. |
| Does not store the current state. | Stores the current state of iteration. |
| An instance of the iterator interface should be produced every time iterator() method is called. | No such contract for iterator interface. |
| Moves only in the forward direction. | Moves in the forward direction and subinterfaces like listIterator support bidirectional traversing. |
| Does not provide any method to modify the elements during iteration. | Provides the remove method that can remove element when iteration is in progress. |
Iterable vs Iterator interfaces in Java.
Reviewed by 13thGhost
on
10:50 AM
Rating:
No comments: