Difference between Iterator and Iterable in Java
This post will discuss differences between the Iterator
and Iterable
in Java.
Both Iterator
and Iterable
are interfaces in Java that look 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 that can be traversed. Implementing the Iterable
interface allows an object to make use of the for-each loop. It does that by internally calling the iterator()
method on the object. For example, the following code only works as a List
interface extends the Collection
interface, and the Collection
interface extends the Iterable
interface.
1 2 3 4 5 |
List<String> persons = new ArrayList<>(Arrays.asList("A", "B", "C")); for (String person: persons) { System.out.println(person); } |
Please note that we can also call the 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.
On the other hand, Iterator
is an interface that allows us to iterate over some other object, which is a collection of some kind. To iterate over an Iterator
, we can use hasNext() + next()
methods in a while loop, as shown below:
1 2 3 4 |
Iterator<Integer> `Iterator` = Arrays.asList(1, 2, 3, 4, 5).iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } |
Starting from Java 8, we can also easily iterate over an Iterator
by using the forEachRemaining()
method.
1 2 |
Iterator<Integer> `Iterator` = Arrays.asList(1, 2, 3, 4, 5).iterator(); iterator.forEachRemaining(System.out::println); |
One can also use an Iterator
inside a for-each loop by wrapping converting the Iterator into an Iterable by using a lambda:
1 2 3 |
for (Integer i: (Iterable<Integer>) () -> iterator) { System.out.println(i); } |
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 the current element, check if the 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 the next item in sequence if present. The Iterable
, on the other hand, doesn’t maintain any such iteration state.
4. The contract for Iterable
is that it should produce a new instance of an Iterator
every time the iterator()
method is called. This is because the Iterator
instance maintains 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
subinterface like ListIterator
allows us to move back and forth over a List
.
Also, Iterable
doesn’t provide any method to modify its elements, nor can we modify them using the for-each loop. But Iterator
allows removing elements from the underlying collection during the iteration with the remove()
method.
That’s all about the differences between Iterator and Iterable in Java.
Implement Iterable Interface to work with for-each loops in Java
Difference between Enumeration and Iterator interface in Java
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)