ListIterator vs Iterator interface

Both Iterator and ListIterator are used to iterate elements of a collection class. Using Iterator we can traverse elements of a collection class  forward direction only  while using ListIterator we can traverse elements of a collection class either forward   and backward directions.

Iterator interface

  • Iterator Interface is used to traverse the element of collection class in forward direction.
  • Iterator is used for traversing List and Set both.
  • We cannot replace the existing element value when using Iterator

 

Commonly used methods of Iterator Interface:

  1. public boolean hasNext() : this method returns true if this Iterator has more element to iterate Otherwise, returns false.
  2. public Object next() :Returns the next element. Throws NoSuchElementException if there is not a next element.
  3. public void remove() :  method remove the last element return by the iterator (optional operation).this method only calls once per call to next().

 

Example of How to use Iterator in Java

import java.util.ArrayList;
import java.util.Iterator;
 
public class MyIteratorDemo {
  public static void main(String args[]){
    ArrayList<String> names = new ArrayList<String>();
    names.add("A");
    names.add("B");
    names.add("C");
 
    Iterator<String> it = names.iterator();
 
    while(it.hasNext()) {
      String obj = it.next();
      System.out.println(obj);
    }
 }
}

Output:

A
B
C

ListIterator Interface

  • ListIterator Interface is used to traverse the element in backward and forward direction.
  •  ListIterator is use  to traverse element of  List only, we cannot traverse element of  Set using ListIterator.
  • we can replace the last element returned by next() or previous() methods of ListIterator interface by using set(E e) method of ListIterator interface

 Methods of ListIterator Interface:

  1. public boolean hasNext() :Returns true if there is a next element. Otherwise, returns false.
  2. public Object next(): Returns the next element. Throws NoSuchElementException if there is not a next element.
  3. public boolean hasPrevious(): Returns true if there is a previous element. Otherwise, returns false.
  4. public Object previous():Returns the previous element. A NoSuchElementException is thrown if there is not a previous element.
  5. public void add(Object obj) : Inserts obj into the list in front of the element that will be returned by the next call to next( ).
  6. public int nextIndex( ) :Returns the index of the next element. If there is not a next element, returns the size of the list.
  7. public int previousIndex( ) :Returns the index of the previous element. If there is not a previous element, returns -1.
  8. public void set(Object obj) : Replaces the last element returned by next() or previous() with the specified element (optional operation).
  9. public void remove() :  method remove the last element return by the iterator (optional operation).this method only calls once per call to next().

 

Example of how to use Iterator in Java

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
 
public class MyListIteratorDemo {
  public static void main(String a[]){
    List<String> list= new ArrayList<String>();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    list.add("E");
    //Obtaining listiterator
    ListIterator<String> litr =list.listIterator();
 
    System.out.println("Traversing the list element in forward direction:");
    while(litr.hasNext()){
       System.out.println(litr.next());
    }
    System.out.println("\nTraversing the list element in backward direction:");
    while(litr.hasPrevious()){
       System.out.println(litr.previous());
    }
  }
}

Output:

Traversing the list element in forward direction:
A
B
C
D
E

Traversing the list element in backward direction:
E
D
C
B
A

			

Leave a comment