Iterator vs ListIterator in Java

18/10/2016 Amit Gupta 563 Java

In this article, we will understand listIterator in Java with examples and will look into important differences between ListIterator and Iterator.

Let us understand about Iterator. Iterator is an interface which is defined in java.util() package.

The main purpose of Iterator is to iterate over various collections like ArrayList, LinkedList e.t.c.

Iterator

public interface Iterator {
    /**
     * Returns {@code true} if the iteration has more elements.
     * (In other words, returns {@code true} if {@link #next} would
     * return an element rather than throwing an exception.)
     *
     * @return {@code true} if the iteration has more elements
     */
    boolean hasNext();

    /**
     * Returns the next element in the iteration.
     *
     * @return the next element in the iteration
     * @throws NoSuchElementException if the iteration has no more elements
     */
    E next();

    /**
     * Removes from the underlying collection the last element returned
     * by this iterator (optional operation).  This method can be called
     * only once per call to {@link #next}.  The behavior of an iterator
     * is unspecified if the underlying collection is modified while the
     * iteration is in progress in any way other than by calling this
     * method.
     *
     * @throws UnsupportedOperationException if the {@code remove}
     *         operation is not supported by this iterator
     *
     * @throws IllegalStateException if the {@code next} method has not
     *         yet been called, or the {@code remove} method has already
     *         been called after the last call to the {@code next}
     *         method
     */
    void remove();
}

As Iterator interface has next() method defined in it, so it is clear that using Iterator we can retrieve the elements from Collection Object in forward direction only.

For example:-

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author Amit Gupta
 *
 */
public class IteratorExample {

	
	public static void main(String[] args) {
		ArrayList arList = new ArrayList();
		
		arList.add("Java 5");
		arList.add("Java 6");
		arList.add("Java 7");
		arList.add("Java 8");
		
		Iterator itr = arList.iterator();
		
		while(itr.hasNext()){
			System.out.println(itr.next());
		}
	}
}

Output

Java 5
Java 6
Java 7
Java 8

Now let us understand ListIterator Interface.

ListIterator

public interface ListIterator extends Iterator {
    
    boolean hasNext();
    
    E next();
    
    boolean hasPrevious();

    E previous();

    int nextIndex();

    int previousIndex();

    void remove();

    void set(E e);

    void add(E e);
}

Using ListIterator you can traverse in either direction(both forward and backward). So it has two additional methods as - hasPrevious() and previous() other than those of Iterator. Also, we can get indexes of the next or previous elements (using nextIndex() and previousIndex() respectively).

For example:-

import java.util.ArrayList;
import java.util.ListIterator;

/**
 * @author Amit Gupta
 *
 */
public class IteratorExample {

	
	public static void main(String[] args) {
		ArrayList arList = new ArrayList();
		
		arList.add("Java 5");
		arList.add("Java 6");
		arList.add("Java 7");
		arList.add("Java 8");
		
		ListIterator itr = arList.listIterator();
		
		System.out.println("--------Traverse the list in forward direction-------->");
	    while(itr.hasNext()){
	       System.out.print(itr.next()+"\t");
	    }
	    System.out.println();
	    System.out.println("<-------Traverse the list in backward direction-------");
	    while(itr.hasPrevious()){
	       System.out.print(itr.previous()+"\t");
	    }
	}
}

Output

--------Traverse the list in forward direction-------->
Java 5	Java 6	Java 7	Java 8	
<-------Traverse the list in backward direction-------
Java 8	Java 7	Java 6	Java 5	

In this article, we learnt about listIterator in Java with examples and will look into important differences between ListIterator and Iterator.

Author
Author: Amit Gupta
Published On: 18/10/2016
Last revised On: 18/10/2016
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media