Javatpoint Logo
Javatpoint Logo

Difference Between Iterator and ListIterator in Java with Example

In Java, iterators provide a way to traverse collections and perform operations on their elements. The Java Collections Framework offers two common iterator types: Iterator and ListIterator. While both iterators serve a similar purpose, they have some important differences that make each suitable for specific scenarios. In this section, we will discuss the distinctions between Iterator and ListIterator in Java with examples to illustrate their usage.

Iterator

The Iterator interface is part of the Java Collections Framework and provides a generic way to traverse collections such as lists, sets, and queues. Here are the key characteristics of an Iterator:

  • Forward-only traversal: An Iterator allows sequential access to elements in a collection in a forward direction.
  • Read and remove operations: It offers methods like next() to retrieve the next element and remove() to remove the last element returned by the next() method.
  • Limited functionality: Unlike ListIterator, the Iterator interface does not support bidirectional traversal or modification of elements during iteration.

Example

Let's consider a simple example to demonstrate the usage of an Iterator:

In the above example, we create an Iterator object using the iterator() method of the ArrayList. We then iterate over the elements using a while loop and retrieve each element using the next() method.

ListIterator

The ListIterator interface extends the Iterator interface and provides additional functionality specifically designed for lists. Here are the key characteristics of a ListIterator:

Bidirectional traversal: Unlike Iterator, ListIterator allows traversing the list in both forward and backward directions.

Read, remove, replace, and add operations: ListIterator supports all the operations of Iterator, along with methods like hasPrevious(), previous(), set(), and add(). These methods enable modifications to the list during iteration.

Example

Consider the following example that demonstrates the usage of ListIterator:

In the above example, we create a ListIterator object using the listIterator() method of the ArrayList. We traverse the list in the forward direction using the next() method, and then in the backward direction using the previous() method.

Iterator Vs. ListIterator

Feature Iterator ListIterator
Interface Iterator is a more general-purpose interface. ListIterator is a subinterface of Iterator and extends it, providing additional methods specific to lists.
Traversal Direction Supports forward-only traversal. Supports both forward and backward traversal.
Access to Elements Read-only access to elements. Read and write access to elements.
Methods
  • boolean hasNext()
  • E next()
  • void remove() (removes last element returned by next())
  • Not applicable
  • boolean hasNext()
  • E next()
  • boolean hasPrevious()
  • previous()
  • int nextIndex()
  • int previousIndex()
  • void set(E e) (replaces last element returned by next() or previous())
  • void add(E e) (inserts e into the list)
Applicability Suitable for traversing any collection. Specifically designed for lists (e.g., ArrayList, LinkedList).
Use Cases When only forward traversal is required. When both forward and backward traversal, along with element modification, are needed.
Supported Collections Works with any collection that implements the Iterable interface. Works with collections that implement the List interface.

Here is a complete Java program that demonstrates the usage of Iterator and ListIterator:

IteratorVsListIteratorExample.java

Output:

Using Iterator:
Apple
Banana
Orange

Using ListIterator:
Apple
Banana
Orange
Orange
Banana
Apple

Explanation

We create an ArrayList called fruits and add three fruit names: "Apple", "Banana", and "Orange". We use an Iterator to traverse the fruits list. We retrieve the Iterator object by calling the iterator() method on the fruits list. We obtain a ListIterator object by calling the listIterator() method on the fruits list. After the forward iteration, we enter another while loop to iterate backward using the hasPrevious() and previous() methods of the ListIterator. This time, the fruits are printed in reverse order.

Conclusion

Iterator and ListIterator are both powerful tools for traversing and manipulating collections in Java. The Iterator interface is suitable for forward-only traversal and basic read and remove operations, while the ListIterator interface provides bidirectional traversal and additional methods for inserting, replacing, and modifying elements in lists. Choosing between the two depends on the specific requirements of your program. By understanding their differences and capabilities, you can select the appropriate iterator type for your needs and write more efficient and effective Java code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA