Friday, 1 January 2016

Difference between Iterator and ListIterator

Iterator and ListIterator are part of Java collection framework, used to traverse the collection. ListIterator interface extends the functionality of Iterator interface.

Following are the difference between Iterator and ListIterator.

a. Traversal
By using ListIterator, you can traverse the collection in both forward and backward directions, where as Iterator supports only forward direction.
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class IteratorDemo {
  public static void main(String args[]) {
    List<String> rivers = new LinkedList<>();

    rivers.add("Nile");
    rivers.add("Amazon");
    rivers.add("Mississippi-Missouri-Red Rock");
    rivers.add("Chang Jiang (Yangtze)");
    rivers.add("Ob");
    rivers.add("Huang Ho (Yellow)");
    rivers.add("Yenisei");
    rivers.add("Parana");

    /* Traverse using Iterator */
    Iterator<String> iter = rivers.iterator();

    System.out.println("Traverse using Iterator");
    int i = 1;
    while (iter.hasNext()) {
      System.out.print(i + "." + iter.next() + "\t");
      i++;
    }

    /* Traverse forward using ListIterator */
    ListIterator<String> listIter = rivers.listIterator();

    System.out.println("\nTraverse forward using ListIterator");
    i = 1;
    while (listIter.hasNext()) {
      System.out.print(i + "." + listIter.next() + "\t");
      i++;
    }

    /* Traverse backward using ListIterator */
    System.out.println("\nTraverse backward using ListIterator");
    i--;
    while (listIter.hasPrevious()) {
      System.out.print(i + "." + listIter.previous() + "\t");
      i--;
    }

    rivers.listIterator();
  }
}


Output
Traverse using Iterator
1.Nile  2.Amazon  3.Mississippi-Missouri-Red Rock 4.Chang Jiang (Yangtze) 5.Ob  6.Huang Ho (Yellow) 7.Yenisei 8.Parana  
Traverse forward using ListIterator
1.Nile  2.Amazon  3.Mississippi-Missouri-Red Rock 4.Chang Jiang (Yangtze) 5.Ob  6.Huang Ho (Yellow) 7.Yenisei 8.Parana  
Traverse backward using ListIterator
8.Parana  7.Yenisei 6.Huang Ho (Yellow) 5.Ob  4.Chang Jiang (Yangtze) 3.Mississippi-Missouri-Red Rock 2.Amazon  1.Nile  


Following table summarizes the description of iterator methods I used.

Method
Description
boolean hasNext()
Returns true if the iteration has more elements, else false.
E next()
Returns the next element in the iteration.
boolean hasPrevious()
This method supported by ListIterator. Returns true if this list iterator has more elements when traversing the list in the reverse direction.
E previous()
This method supported by ListIterator. Returns the previous element in the list and moves the cursor position backwards.


b. Add element while traversing
By using ListIterator, you can add new elements while traversing the list. The element is inserted immediately before the element that would be returned by next() method. if any, and after the element that would be returned by previous(), if any.

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class IteratorDemo {
  public static void main(String args[]) {
    List<Integer> primes = new LinkedList<>();

    primes.add(3);
    primes.add(5);
    primes.add(11);
    primes.add(13);
    primes.add(17);

    System.out.println("Elements in list are");
    ListIterator<Integer> listIter = primes.listIterator();

    while (listIter.hasNext()) {
      System.out.print(listIter.next() + "\t");
    }

    System.out.println("\nAdding elements to list while iterating");
    listIter = primes.listIterator();
    int i = 0;
    while (listIter.hasNext()) {
      if (i == 0) {
        listIter.add(2);
      }

      if (i == 2) {
        listIter.add(7);
      }

      i++;

      listIter.next();
    }
    listIter.add(19);

    listIter = primes.listIterator();
    while (listIter.hasNext()) {
      System.out.print(listIter.next() + "\t");
    }

  }
}


Output

Elements in list are
3 5 11  13  17  
Adding elements to list while iterating
2 3 5 7 11  13  17  19


c. Update the elements
By using set method of ListIterator, we can update the elements while traversing. Replaces the last element returned by next method or previous method with the specified element.

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class IteratorDemo {
  public static void main(String args[]) {
    List<Integer> primes = new LinkedList<>();

    primes.add(2);
    primes.add(3);
    primes.add(5);
    primes.add(7);
    primes.add(11);

    System.out.println("Elements in list are");
    ListIterator<Integer> listIter = primes.listIterator();

    while (listIter.hasNext()) {
      System.out.print(listIter.next() + "\t");
    }

    System.out.println("\nUpdating elements of list while iterating");
    listIter = primes.listIterator();
    int i = 0;
    while (listIter.hasNext()) {
      listIter.next();
      listIter.set(i);
      i++;
    }

    listIter = primes.listIterator();
    while (listIter.hasNext()) {
      System.out.print(listIter.next() + "\t");
    }

  }
}


Output

Elements in list are
2 3 5 7 11  
Updating elements of list while iterating
0 1 2 3 4


d. Get the index of the element while traversing
ListIterator provide following methods to get the index of next and previous elements while traversing.

Method
Description
int nextIndex()
Returns the index of the element that would be returned by a subsequent call to next() method.
int previousIndex()
Returns the index of the element that would be returned by a subsequent call to previous() method.

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class IteratorDemo {
  public static void main(String args[]) {
    List<Integer> primes = new LinkedList<>();

    primes.add(2);
    primes.add(3);
    primes.add(5);
    primes.add(7);
    primes.add(11);

    System.out.println("Index \tElement");
    ListIterator<Integer> listIter = primes.listIterator();

    while (listIter.hasNext()) {
      int ele = listIter.next();
      int index = listIter.nextIndex() - 1;

      System.out.println(index + "\t" + ele);
    }
  }
}


Output

Index  Element
0 2
1 3
2 5
3 7
4 11


e. You can get ListIterator from particular index.

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class IteratorDemo {
  public static void main(String args[]) {
    List<Integer> primes = new LinkedList<>();

    primes.add(2);
    primes.add(3);
    primes.add(5);
    primes.add(7);
    primes.add(11);

    System.out.println("Index \tElement");
    ListIterator<Integer> listIter = primes.listIterator(2);

    while (listIter.hasNext()) {
      int ele = listIter.next();
      int index = listIter.nextIndex() - 1;

      System.out.println(index + "\t" + ele);
    }
  }
}


Output

Index  Element
2 5
3 7
4 11


You may like





                                                 Home

No comments:

Post a Comment