Java Iterator is an important tool, which allows developers to traverse elements in a collection in a simple and consistent way.
In this chapter, you will learn about the Java Iterator, its purpose, features, and how it is used to iterate over elements in a collection.
An Iterator is an interface used to traverse elements of a collection one by one. It is part of the Java Collections Framework and belongs to the java.util package. The Iterator interface was introduced in Java 1.2.
Iterator provides a standard way to access elements in collections such as ArrayList, HashSet, and other collection classes. It is called a universal cursor because it can be used with all classes that implement the Collection interface.
Before Iterator, Java used the Enumeration interface to traverse elements. However, Enumeration was limited and only supported read operations. Iterator improved upon Enumeration by providing additional functionality, such as the ability to remove elements during iteration.
The Iterator interface mainly supports the following operations:
Compared to Enumeration, the method names in Iterator are more clear, consistent, and easier to use that make it the preferred choice for traversing collections in modern Java programming.
To use a Java Iterator, first obtain an Iterator instance from a collection (such as ArrayList or HashSet). The Iterator then allows you to traverse the elements one by one.
The Iterator keeps track of the current position in the collection, ensuring each element is accessed sequentially.
If the collection is modified while iterating (except through the Iterator’s own remove() method), it will throw a ConcurrentModificationException during the next operation.
The Java Iterator interface provides methods to traverse and modify elements in a collection. It contains a total of four methods:
This method does not accept any parameters. It returns true if there are more elements remaining in the iteration; otherwise, it returns false.
If there are no more elements, there is no need to call the next() method. In simple terms, this method is used to determine whether next() should be called or not.
The syntax of this method is:
This method does not accept any parameters. It returns the next element (E) in the traversal.
If there are no more elements in the iteration, it throws a NoSuchElementException.
The syntax of this method is:
This method does not accept any parameters and does not return any value. It removes the last element returned by the iterator from the underlying collection.
The remove() method can be called only once per call to next().
The syntax of this method is:
This method accepts a parameter action, which represents the operation to be performed on each remaining element. It does not return any value.
This method processes all remaining elements in the collection until all elements are consumed or an exception occurs.
The forEachRemaining() method was introduced in Java 8.
The syntax of this method is:
In this example, we create an ArrayList of city names. Then, we initialize an Iterator using the iterator() method of the ArrayList. After that, we traverse the list to display each element one by one.
Output:
CityNames elements: Delhi Mumbai Kolkata Chandigarh Noida
We request you to subscribe our newsletter for upcoming updates.