Java Collection Framework - ListIterator Interface

Java Collection Framework - ListIterator Interface


Posted in : Core Java Posted on : January 24, 2011 at 5:39 PM Comments : [ 0 ]

A ListIterator is implemented only on List interface through different ways. In java ListIterator rotates all the elements sequentially in both direction of the list.

Java Collection Framework - ListIterator Interface

Introduction to ListIterator<E> interface in java

A ListIterator is implemented only on List interface through different ways. In java ListIterator rotates all the elements sequentially in both direction of the list. It performs the process of cycling all the elements in both directions; list can be modified during the iteration, and can also obtained the iterator's current position in the list. ListIterator interface does not contain any current element; invariably its cursor is positioned between the element that would be gave back by a call either previous( ) or next(). ListIterator interface is different from Iterator by following ways : 

* In ListIterator interface process of cycling is done in two-way direction whereas in Iterator this process is one-way.

* ListIterator interface supports more methods than the Iterator interface. 

Syntax

public interface ListIterator<E>

In java this interface is used for cycling to all the elements in both direction of a collection in a sequence. In this interface the process of cycling is done in both forward and backward directions. Through this interface we can find the next element, previous element, replace the element, remove the element, add an element, elements index position etc. from a collection. The ListIterator interface is implemented by listIterator( ) method. The listIterator( ) method rotates one element at a time in both forward and backward directions in a collection in sequential manner.

Methods of ListIterator

A ListIterator traverse bi-directionally, and uses some methods for traversing all the elements in a collection. For traversing in both directions ListIterator requires at least one of the following methods :

hasNext( ) - While we are using the List Iterator, this method is implemented for searching the next element in forward direction and positioned the cursor at that element in list so it returns true if the iteration has more elements in next.

next( )  - This method returns the next element in the iteration. So if you want to find that element  from list you may use this method.

nextIndex( ) - This method returns the position (index) of that element which would be returned by the subsequent call to next.

hasPrevious( ) - hasPrevious( ) method is implemented for searching the previous element in backward direction and positioned the cursor at that element in list. It returns true if the iteration has more elements in previous position in the list.

previous( ) - This method returns the previous element in the iteration. So if you want to find that element  from list you may use this method.

previousIndex( ) - This method returns the position (index) of that element which would be returned by the subsequent call to previous.

add(Object o) - This method adds a specific element into the list.

remove( ) - This method removes the last element from list which was gave back by the next or previous method.

set(Object o) - This method is used to substitute the last element, gave back by the next or previous method, with a specific element.

Implemetation of ListIterator

Here I will discuss how to implement the interface ListIterator.

import java.util.ListIterator : This interface is used only for List interface and it provides bi-directional traversal in the list.

To use a ListIterator in any  list, follow these steps :

* Find an iterator to the start of the collection by calling the collection's listIterator( ) method.

ListIterator litr =lis.listIterator( ); // Here lis is an object of any type of List

* Set a loop that makes a call to litr.hasNext( ). It returns true if list contains more element while traversing in the forward direction.

*  To find an element from list call litr.next( ) method within the loop in forward direction.

* Set a loop that makes a call to litr.hasPrevious( ). It returns true if list contains more elements while traversing in the backward direction.

* To find an element from list call litr.previous ( ) method within the loop in backward direction.

Example of ListIterator interface

In this example we will show you the implementation of ListIterator interface and its methods. Here we will show how the above methods may be used with this interface. Through this example we will show you how does cycling process perform in forward and backward directions, how can next and previous element be fetched from a collection.

Example :-

 
package Devmanuals.com;

import java.util.List;

import java.util.LinkedList;

import java.util.ListIterator;

public class ListIteratorInterface {

  public static void main(String args[]) {

    List llist = new LinkedList();

    llist.add("C");

    llist.add("C++");

    llist.add("Java");

    llist.add("DBMS");

    llist.add("RDBMS");

    // Implementation of ListIterator

    ListIterator litr = llist.listIterator();

    System.out.println("List has the elements in forward : "

        + litr.hasNext());

    while (litr.hasNext()) {

      System.out.println(litr.next());

    }

    System.out.println("\nList has the elements in backward : "

        + litr.hasPrevious());

    while (litr.hasPrevious()) {

      System.out.println(litr.previous());

    }

  }

}
   

Output :

List has the elements in forward : true
C
C++
Java
DBMS
RDBMS
List has the elements in backward : true
RDBMS
DBMS
Java
C++
C

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics