What is the Difference Between Iterator and ListIterator

The main difference between Iterator and ListIterator is that Iterator can traverse the elements in the collection only in forward direction while ListIterator can traverse the elements in the collection in both forward and backward directions.

Iterator and ListIterator are two interfaces in Java. Iterator is used with Lists, Sets, and Maps. On the other hand, ListIterator is only used with Lists. In ListIterator, it is possible to go through the items in the collection in forward and backward directions. In contrary, the Iterator can only go through the items in the collection in forward direction.

Key Areas Covered

1. What is Iterator
     – Definition, Functionality
2. What is ListIterator
     – Definition, Functionality
3. What is the Difference Between Iterator and ListIterator
     – Comparison of Key Differences

Key Terms

Collection, Interface, Iterator, ListIterator

Difference Between Iterator and ListIterator - Comparison Summary

What is Iterator

Iterator is an interface in the Collection framework. It allows going through each element in a collection one after the other. It helps to traverse Lists, Sets, and Maps. However, it is only possible to traverse forward using the Iterator. An example program is as follows.

Difference Between Iterator and ListIterator

Figure 01: Java Program with Iterator

In the above program, there is an ArrayList called colors. The add method helps to insert items to the collection. The iterator() returns the iterator to the beginning of the collection. The while loop calls the hasNext() method every time the loop iterates. If there is a next element available in the collection, this method returns true. However, it returns false if all the elements are already traversed. Inside the loop, the next() method helps to obtain each element in the Collection. It returns the next element of the collection. The System.out.println displays the element on the console.

What is ListIterator

ListIterator is an interface in the Collection framework that allows traversing through the elements present in a collection one after the other. It can only traverse the items in a List. Moreover, it is possible to traverse the elements in both forward and backward directions. Refer below program.

What is the Difference Between Iterator and ListIterator

Figure 02: Java program with ListIterator

In the above program, there is an arraylist called colors. The add method helps to insert elements to the collection.  The listIterator() returns the iterator to the beginning of the collection. The first while loop calls the hasNext() method in every iteration. Inside the loop, the next() method is used to return the next element of the collection. The System.out.println displays the element on the console. Therefore, all elements are displayed on the console in the forward direction.

The next while loop calls the hasPrevious() method each time the loop iterates. It returns true if the elements are available. If not, it returns false. This traversing occurs in a backward direction. Inside the loop, the previous() method helps to obtain each element in the Collection. It returns the previous element of the collection. The System.out.println displays the element on the console.

Difference Between Iterator and ListIterator

Definition

Iterator is an interface in Collection framework of Java to traverse the list in the forward direction. In contrast, ListIterator is an interface in Collection framework of Java to traverse a list in either direction, modify the list during iteration, and obtain the current position in the list. Thus, this is the main difference between Iterator and ListIterator.

Traverse

An important difference between Iterator and ListIterator is that Iterator can traverse Lists, Sets, and Maps while ListIterator can only traverse Lists.

Traversing Direction

Moreover, Iterator can only traverse the collection in the forward direction while ListIterator can traverse the collection in both forward and backward directions. Hence, this is also a difference between Iterator and ListIterator.

Index

Furthermore, another difference between Iterator and ListIterator is that there is no method in Iterator to obtain an index of the element. However, it is possible to obtain the index of the element using ListIterator.

Add Elements

While Iterator cannot add elements to the collection, ListIterator can add elements to the collection using add(E e). So, this is another difference between Iterator and ListIterator.

Modify

Also, one other difference between Iterator and ListIterator is that Iterator cannot modify the elements in the collection. whereas ListIterator can modify the elements in the collection using set(E e).

Common Methods

hasNext(), next(), remove() are some common methods of Iterator while hasNext(), next(), hasPrevious(), previous(), remove, add(E e), previousIndex(), set(E e) are some common methods of ListIterator.

Conclusion

There is a distinct difference between Iterator and ListIterator although some people use these terms interchangeably. The main difference between Iterator and ListIterator is that Iterator can traverse the elements in the collection only in forward direction while ListIterator can traverse the elements in the collection in both forward and backward directions.

Reference:

1. Singh, Chaitanya. “Java Iterator with Examples.” Beginnersbook.com, 15 June 2014, Available here.
2. Singh, Chaitanya, and Umesh. “ListIterator in Java with Examples.” Beginnersbook.com, 15 June 2014, Available here.
3. ListIterator (Java Platform SE 7 ), 6 Oct. 2018, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply