Iterator Interface Java
Iterator Interface Java
In Java, an Iterator is one of the Java cursors. Java Iterator is an interface that is practiced in order to iterate
over a collection of Java object components entirety one by one. It is free to use in the Java programming
language since the Java 1.2 Collection framework. It belongs to java.util package.
Though Java Iterator was introduced in Java 1.2, however, it is still not the oldest tool available to traverse
through the elements of the Collection object. The oldest Iterator in the Java programming language is the
Enumerator predated Iterator. Java Iterator interface succeeds the enumerator iterator that was practiced in the
beginning to traverse over some accessible collections like the ArrayLists.
The Java Iterator is also known as the universal cursor of Java as it is appropriate for all the classes of the
Collection framework. The Java Iterator also helps in the operations like READ and REMOVE. When we compare
the Java Iterator interface with the enumeration iterator interface, we can say that the names of the methods
available in Java Iterator are more precise and straightforward to use.
o The user can apply these iterators to any of the classes of the Collection framework.
o In Java Iterator, we can use both of the read and remove operations.
o If a user is working with a for loop, they cannot modernize(add/remove) the Collection, whereas, if they
use the Java Iterator, they can simply update the Collection.
o The Java Iterator is considered the Universal Cursor for the Collection API.
o The method names in the Java Iterator are very easy and are very simple to use.
o The Java Iterator only preserves the iteration in the forward direction. In simple words, the Java Iterator is
a uni-directional Iterator.
o The replacement and extension of a new component are not approved by the Java Iterator.
o In CRUD Operations, the Java Iterator does not hold the various operations like CREATE and UPDATE.
o In comparison with the Spliterator, Java Iterator does not support traversing elements in the parallel
pattern which implies that Java Iterator supports only Sequential iteration.
o In comparison with the Spliterator, Java Iterator does not support more reliable execution to traverse the
bulk volume of data.
o hasNext()
o next()
o remove()
o forEachRemaining()
The forEachRemaining() method was added in the Java 8. Let's discuss each method in detail.
o boolean hasNext(): The method does not accept any parameter. It returns true if there are more
elements left in the iteration. If there are no more elements left, then it will return false.
If there are no more elements left in the iteration, then there is no need to call the next() method. In
simple words, we can say that the method is used to determine whether the next() method is to be called
or not.
o next(): It is similar to hasNext() method. It also does not accept any parameter. It returns E, i.e., the next
element in the traversal. If the iteration or collection of objects has no more elements left to iterate, then
it throws the NoSuchElementException.
o default void remove(): This method also does not require any parameters. There is no return type of this
method. The main function of this method is to remove the last element returned by the iterator
traversing through the underlying collection. The remove () method can be requested hardly once per the
next () method call. If the iterator does not support the remove operation, then it throws the
UnSupportedOperationException. It also throws the IllegalStateException if the next method is not yet
called.
o default void forEachRemaining(Consumer action): It is the only method of Java Iterator that takes a
parameter. It accepts action as a parameter. Action is nothing but that is to be performed. There is no
return type of the method. This method performs the particularized operation on all of the left
components of the collection until all the components are consumed or the action throws an exception.
Exceptions thrown by action are delivered to the caller. If the action is null, then it throws a
NullPointerException.
JavaIteratorExample.java
import java.io.*;
import java.util.*;
public class JavaIteratorExample {
public static void main(String[] args)
{
ArrayList<String> cityNames = new ArrayList<String>();
cityNames.add("Delhi");
cityNames.add("Mumbai");
cityNames.add("Kolkata");
cityNames.add("Chandigarh");
cityNames.add("Noida");
while (iterator.hasNext())
System.out.print(iterator.next() + " ");
System.out.println();
}
}
Output:
CityNames elements:
Delhi Mumbai Kolkata Chandigarh Noida
Points to Remember
o The Java Iterator is an interface added in the Java Programming language in the Java 1.2 Collection
framework. It belongs to java.util package.
o It is one of the Java Cursors that are practiced to traverse the objects of the collection framework.
o The Java Iterator is used to iterate the components of the collection object one by one.
o The Java Iterator is also known as the Universal cursor of Java as it is appropriate for all the classes of the
Collection framework.
o The Java Iterator also supports the operations like READ and REMOVE.
o The methods names of the Iterator class are very simple and easy to use compared to the method names
of Enumeration Iterator.
1. It can traverse over all collections like ArrayList, HashMap, TreeSet, HashSet etc.
2. It can be used for any java collection and therefore known as Universal Cursor for Collection API.
3. Read and Remove operations are supported.
4. Simple and easily usable method names.
1. It does not support Create and Update operations in CRUD (Create, Read, Update, Delete) operations.
2. It supports only uni-directional traversal i.e in forwarding direction.
3. It does not support a better iteration on a large volume of data.
4. It supports only sequential iteration i.e it does not support iterating elements parallel.