Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

ListIterator

ListIterator is an interface in the java.util package that extends the java.util.Iterator interface.

java.util.ListIterator:

public interface java.util.ListIterator<E> extends java.util.Iterator<E> {
public abstract boolean hasNext();
public abstract E next();
public abstract boolean hasPrevious();
public abstract E previous();
public abstract int nextIndex();
public abstract int previousIndex();
public abstract void remove();
public abstract void set(E);
public abstract void add(E);
}

Important points of ListIterator:

1. While an Iterator can iterate through any collection, such as a list, set, and queue, a ListIterator is specifically designed for iterating over list-type collections.
2. Using ListIterator, we can iterate a list in both forward and reverse orders. However, to iterate a list in reverse order, we must always first iterate the list in the forward order.
3. While iterating a list in forward or reverse order using ListIterator, performing add and remove operations with the list using the add() and remove() methods of java.util.ArrayList class leads to a java.util.ConcurrentModificationException.

            
import java.util.ArrayList;
importjava.util.Collection;
importjava.util.Collections;
importjava.util.Iterator;
import java.util.ListIterator;
publicclass JTC {
   publicstaticvoid main(String[] args) {
      ArrayListlist = newArrayList();
      list.add(101);
      list.add(202);
      list.add(303);
      list.add(404);
      list.add(505);
      ListIteratoriterator = list.listIterator();
      System.out.println("-----List in reverse order before forward iteration------");
      while (iterator.hasPrevious()) {
         System.out.println(iterator.previous());
      }
      System.out.println("------List in forward order------");
      while (iterator.hasNext()) {
         System.out.println(iterator.next());
         // list.add("101"); --> java.util.ConcurrentModificationException
         // list.remove(new Integer(101)); --> java.util.ConcurrentModificationException
      }
      System.out.println("-----List in reverse order after forward iteration------");
      while (iterator.hasPrevious()) {
         System.out.println(iterator.previous());
      }
   }
}
   -----List in reverse order before forward iteration------
   ------List in forward order------
   101
   202
   303
   404
   505
   -----List in reverse order after forward iteration------
   505
   404
   303
   202
   101
            

In this example, we create a ListIterator and attempt to iterate the list in reverse order, as discussed, without getting any output. Next, we iterate the list in forward order, and as predicted, we obtain the proper output. Subsequently, we iterate the list in reverse order again, and now we successfully retrieve the elements of the list in reverse order.

Important method of java.util.ListIterator:

1. public abstract boolean hasNext(): It is an abstract method of the java.util.Iterator interface. We use the hasNext() method to check whether the current working collection has more elements to iterate. If yes, then it returns true; otherwise, it returns false.

2. public abstract Object next(): It returns the next element of the list and advances the cursor position. It throws a NoSuchElementException when the ListIterator does not have more elements to iterate.

3. public abstract boolean hasPrevious(): It returns true when the ListIterator has more elements while traversing the list in reverse order; otherwise, it returns false.

4. public abstract E previous() : It returns the previous element in the list and moves the cursor backward. It throws a NoSuchElementException when the ListIterator does not have more previous elements to iterate.

5. public abstract int nextIndex() : It returns the index of the element that would be returned by the subsequent call to the next() method. For example, at the beginning, it returns 1, and at the last, it returns the length of the list.

6. public abstract int previousIndex() : It returns the index of the element that would be returned by the subsequent call to the previous() method. When the iterator is at the beginning, it returns -1.

7. public abstract void remove(): It removes from the list the last element that was returned by next() or previous(). This call can only be made once per call to next() or previous() method. It can be made only if add has not been called after the last call to next() or previous() method. It throws UnsupportedOperationException if the remove operation is not supported by this list iterator and throws IllegalStateException if neither next() nor previous() have been called, or remove or add have been called after the last call to next() or previous().

8. public abstract void set(Object) :- It replaces the last element returned by next() or previous() with the specified element. This call can be made only if neither remove() nor add() has been called after the last call to next() or previous(). It throws:

UnsupportedOperationException - If the set operation is not supported by this list iterator.

ClassCastException - If the class of the specified element prevents it from being added to this list.

IllegalArgumentException - If some aspect of the specified element prevents it from being added to this list.

IllegalStateException - If neither next() nor previous() have been called, or if remove() or add() have been called after the last call to next() or previous().

9. public abstract void add(Object): It inserts the specified element into the list. The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any. If the list contains no elements, the new element becomes the sole element on the list. The new element is inserted before the implicit cursor: a subsequent call to next() would be unaffected, and a subsequent call to previous() would return the new element. (This call increases by one the value that would be returned by a call to nextIndex() or previousIndex().) It throws:

UnsupportedOperationException - If the add method is not supported by this list iterator.

ClassCastException - If the class of the specified element prevents it from being added to this list.

IllegalArgumentException - If some aspect of this element prevents it from being added to this list.