Differences between Iterator and Iterable in Java

In this post, we will discuss the differences between Iterator and Iterable in Java.


 

Both Iterator and Iterable are interfaces provided by Java’s Collection Framework which looks very similar and are often confusing for beginners but both are two different things. In short, if any class implements the Iterable interface, it gains the ability to iterate over an object of that class using an Iterator.

 
Now let’s discuss the major differences between the two in detail –

 
1. An Iterable represents a collection which can be traversed. Implementing the Iterable interface allows an object to make use of for-each loop. It does that by internally calling the iterator() method on the object. For example, below code only works as List interface extends the Collection interface and Collection interface extends the Iterable interface.

Please note that we can also call forEach() method on an Iterable starting from Java 8, which performs the given action for each element of the Iterable. It is worth noting that the default implementation of forEach() also uses for-each internally.

 
An Iterator, on the other hand, is an interface which allows us to iterate over some other object which is a collection of some kind. In order to iterate over an Iterator, we can use hasNext() + next() methods in a while loop as shown below:

 
Starting from Java 8 we can also easily iterate over an Iterator by using forEachRemaining() method.

 
One can also use an Iterator inside a for-each loop by wrapping converting the Iterator into an Iterable by using a lambda:

 
 
2. Any class that implements the Iterable interface needs to override the iterator() method provided by the Iterable interface. The iterator() method returns an Iterator which then can be used to iterate over an object of that class.

Any class implementing the Iterator interface needs to override the hasNext() and next() methods provided by the Iterator interface. The hasNext() method returns true if the iteration has more elements and the next() method returns the next element in the iteration.

 
 
3. The Iterator instance stores the iteration state. That means it provides utility methods to get current element, check if next element exists and move forward to the next element if present. In other words, an Iterator remembers the current position in a collection and returns next item in sequence if present. The Iterable, on the other hand, don’t maintain any such iteration state.

 
 
4. The contract for Iterable is that it should produce a new instance of an Iterator every time iterator() method is called. This is because the Iterator instance maintain the iteration state, and things won’t work as if the implementation returns the same Iterator twice.

 
 
5. For an Iterable, we can move forward only in the forward direction but some of the Iterator’s sub-interfaces like ListIterator allows us to move back and forth over a List.

Also, Iterable doesn’t provide any method to modify its elements, nor we can modify them using for-each loop. But Iterator allows to remove elements from the underlying collection during the iteration with the help of remove() method.
 

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz