ListIterator interface

ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements. It is a sub interface of Iteration which adds the functionality of by directional and traversing of list elements.
The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.
ListIteration Constructor :
  1. public ListIterator listIterator();
  2. public ListIterator listIterator(int index) ;
Method of ListIteration Interface
  1. void add(Object obj)
    Inserts obj into the list in front of the element that will be returned by the next call to next( ).
  2. boolean hasNext( )
    Returns true if there is a next element. Otherwise, returns false.
  3. boolean hasPrevious( )
    Returns true if there is a previous element. Otherwise, returns false.
  4. Object next( )
    Returns the next element. A NoSuchElementException is thrown if there is not a next element.
  5. int nextIndex( )
    Returns the index of the next element. If there is not a next element, returns the size of the list.
  6. Object previous( )
    Returns the previous element. A NoSuchElementException is thrown if there is not a previous element.
  7. int previousIndex( )
    Returns the index of the previous element. If there is not a previous element, returns -1.
  8. void remove( )
    Removes the current element from the list. An IllegalStateException is thrown if remove( ) is called before next( ) or previous( ) is invoked.
  9. void set(Object obj)
    Assigns obj to the current element. This is the element last returned by a call to either next( ) or previous( ).
Processing element using ListIterator Interface
With the help of iterator object, we can access each element in the collection, one element at a time. In general, to use an iterator to cycle through the contents of a collection, follow these three steps:
  1. Create an reference of ListIterator Interface for obtain an listIterator to the start of the collection by calling the collection’s listIterator( ) method.
  2. Set up a loop that makes a call to hasNext( ). The loop will be continue as long as hasNext( ) returns true.
  3. Within the loop, obtain each element by calling next( ).
Example with Java 5 generics for getting element form list or set

An iterator might be used as follows. ArrayList<String> arraylist = new ArrayList<String>(); // . . . Add Strings to arraylist ListIterator<String> it = arraylist.ListIterator(); while( it.hasNext() ) { String str =; // No downcasting required. System.out.println(str); }
Example of getting element form list or set using enchanced for loop.

for (String str : arraylist) { System.out.println(str); }
Example pre Java 5, with explicit listIterator and downcasting

An iterator might be used as follows. ArrayList arraylist = new ArrayList(); // Hold as an object // . . . Add Strings to arraylist ListIterator it = arraylist.listIterator(); while( it.hasNext() ) { String str =(String); // Downcasting is required pre java 5 System.out.println(str); }
Example of ListIterator interface
import java.util.List; import java.util.ArrayList; import java.util.ListIterator; public class ListIteratorDemo{ public static void main(String[] args) { // LinkedList Creation List list = new ArrayList(); // Adding elements to the List list.add("101"); list.add("102"); list.add("103"); list.add("104"); list.add("105"); list.add("106"); // Display content of List System.out.println("Content of List " + list); // Display content of List using iterator ListIterator li=list.listIterator(); System.out.println("Content of List using listIterator : "); while(li.hasNext()) { System.out.println(; } // Modify objects being iterated li=list.listIterator(); while(li.hasNext()) { li.set("20" ); } System.out.println("Modified contents of list: "); li=list.listIterator(); while(li.hasNext()) { System.out.println(; } System.out.println(); // Now, display the list backwards System.out.print("Modified list backwards: "); while(li.hasPrevious()) { System.out.print(li.previous() + " "); } System.out.println(); } }

Content of List [101, 102, 103, 104, 105, 106]
Content of List using listIterator :
Modified contents of list:

Modified list backwards:

Download this example


Latest Tutorial

Most Visited Tutorial