Iterator in Java: Understanding the Fundamentals of Java Iterator

While working in Java, you may have faced a situation where you need to cycle through all the elements of a collection. For example, you may wish to display the elements one by one or retrieve each of the elements. You can accomplish these tasks with ease by employing a Java iterator. And, this brings us to the question, what is an iterator in Java?

What is an Iterator in Java?

An ‘iterator’ is an interface that belongs to the Java Collection framework. It facilitates traversing a collection, accessing data elements, and removing data elements from the collection. In simple terminology, a Java iterator allows one to cycle through a collection of elements. And, at the same time, it obtains or removes specific elements from the given collection. Java has three iterators, namely enumeration, iterator, and list iterator.

Read more: A Guide to Iterator Implementation in Java

Enumeration

Enumeration is a Java iterator interface that allows obtaining elements of the legacy collections like vector and hashtable. The enumeration interface is the first iterator in JDK 1.0, and the rest, more functional ones being included in JDK 1.2. Enumerations are also used for specifying the input streams to a SequenceInputStream. To create the enumeration object, the elements( ) method of the vector class needs to be adopted. There are two methods of the enumeration interface:

  • boolean hasMoreElements( ): It checks the availability of the next element.
  • Object nextElement( ): It returns the next element. 

Iterator

The iterator interface is universal, and hence, it can be applied to any collection object. By using this Java iterator, both reading and removal operations can be carried out. The interface is an improved version of enumeration with more straightforward method names and the additional function of removing elements from collections.

Since it is the only available cursor for the entire Collection framework, the iterator interface can be used whenever there is a need to enumerate the elements in interfaces like List, Set, Queue, and Deque. An iterator also works in all the implemented classes of the Map interface. Creation of the iterator object is done by calling the Iterator( ) method present in the collection interface.

The iterator interface declares three methods:

  • boolean hasNext( ): It returns true in the case there are more elements, or else, returns false.
  • Object next( ): It returns the next element. But, if there is no element next, it throws NoSuchElementException.
  • void remove( ): This method is for the removal of current element. It will throw IllegalStateException if remove( ) is called without first invoking the next ( ) method.

List Iterator

The list iterator type of Java iterator is only applicable for classes like LinkedList and ArrayList that are implemented by List Collection. In contrast to the enumeration and iterator interfaces, the list iterator allows bi-directional iteration and can be used to enumerate the elements of a list. The list interface contains the listIterator( ) method which can be invoked for creating the list iterator object. The list iterator interface is an extension of the iterator interface.

In addition to the three methods present in the iterator, a list iterator contains six more methods. And, these are:

  • void add(Object obj): It adds an object to the list in front of the element which will be returned by the subsequent call to next( ).
  • boolean hasNext( ): It returns true in the case there is any next element. Else, it returns false.
  • boolean hasPrevious( ): It returns true in the case there is a previous element. Else, it returns false.
  • Object next( ): It returns the next element. But, if there is no element next, it throws NoSuchElementException.
  • int nextIndex( ): This method is for returning the index of the next element. It will return the size of the list in case there is no next element.
  • Object previous( ): It returns the previous element. But, if there is no previous element, it throws NoSuchElementException.
  • int previousIndex( ): This method is for returning the index of the previous element. It will return -1 in case there is no next element.
  • void remove( ): This method is for the removal of the current element. It will throw IllegalStateException if remove( ) is called without first invoking the next ( ) method.
  • void set(Object obj): It is for assigning an object to the current element. This is the element that is returned last by a call to either previous( ) or next( ).

Also Read: Java Developer Salary in India

Conclusion

Among the three Java iterators, the enumeration interface is a deprecated one that has been replaced by the more functional iterator and list iterator interfaces. An iterator addresses the limitations of enumeration and is indispensable for any iteration programmer. The basics of Java iterator discussed in this article will help you get started with iterating collections in Java.

If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Enroll Today

Leave a comment

Your email address will not be published. Required fields are marked *

×