ListIterator

It is only applicable for List collection implemented classes like ArrayList, LinkedList, etc. It provides bi-directional iteration. ListIterator must be used when we want to enumerate elements of List. This cursor has more functionality(methods) than iterator. ListIterator object can be created by calling listIterator() method present in the List interface.

Syntax

ListIterator ltr = l.listIterator();

Note: Here “l” is any List object, ltr is of type. ListIterator interface and refers to “l”. ListIterator interface extends the Iterator interface. So all three methods of Iterator interface are available for ListIterator. In addition, there are six more methods. 

1. Forward direction

1.1 hasNext(): Returns true if the iteration has more elements

public boolean hasNext();

1.2 next(): Same as next() method of Iterator. Returns the next element in the iteration. 

public Object next(); 

1.3 nextIndex(): Returns the next element index or list size if the list iterator is at the end of the list. 

public int nextIndex();

2. Backward direction

2.1 hasPrevious(): Returns true if the iteration has more elements while traversing backward.

public boolean hasPrevious();

2.2 previous(): Returns the previous element in the iteration and can throw NoSuchElementException if no more element present.

public Object previous(); 

2.3 previousIndex(): Returns the previous element index or -1 if the list iterator is at the beginning of the list, 

public int previousIndex();

3. Other Methods

3.1 remove(): Same as remove() method of Iterator. Removes the next element in the iteration.

public void remove();

3.2 set(Object obj): Replaces the last element returned by next() or previous() with the specified element.

public void set(Object obj); 

3.3 add(Object obj): Inserts the specified element into the list at the position before the element that would be returned by next()

public void add(Object obj);

Clearly, the three methods that ListIterator inherits from Iterator (hasNext(), next(), and remove()) do exactly the same thing in both interfaces. The hasPrevious() and the previous operations are exact analogs of hasNext() and next(). The former operations refer to the element before the (implicit) cursor, whereas the latter refers to the element after the cursor. The previous operation moves the cursor backward, whereas the next moves it forward.

ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().

1. set() method can throw 4 exceptions. 

  • UnsupportedOperationException: if the set operation is not supported by this list iterator
  • ClassCastException: If the class of the specified element prevents it from being added to this list
  • IllegalArgumentException: If some aspect of the specified element prevents it from being added to this list
  • IllegalStateException: If neither next nor previous have been called, or remove or add have been called after the last call to next or previous

2. add() method can throw 3 exceptions. 

  • UnsupportedOperationException: If the add method is not supported by this list iterator
  • ClassCastException: If the class of the specified element prevents it from being added to this list
  • IllegalArgumentException: If some aspect of this element prevents it from being added to this list

Example

Java
// Java program to demonstrate ListIterator

// Importing ArrayList and List iterator classes
// from java.util package
import java.util.ArrayList;
import java.util.ListIterator;

// Main class
public class Test {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of ArrayList class
        ArrayList al = new ArrayList();

        // Iterating over Arraylist object
        for (int i = 0; i < 10; i++)

            // Adding elements to the Arraylist object
            al.add(i);

        // Print and display all elements inside object
        // created above
        System.out.println(al);

        // At beginning ltr(cursor) will point to
        // index just before the first element in al
        ListIterator ltr = al.listIterator();

        // Checking the next element availability
        while (ltr.hasNext()) {
            //  Moving cursor to next element
            int i = (Integer)ltr.next();

            // Getting even elements one by one
            System.out.print(i + " ");

            // Changing even numbers to odd and
            // adding modified number again in
            // iterator
            if (i % 2 == 0) {
                // Change to odd
                i++;
                // Set method to change value
                ltr.set(i);
                // To add
                ltr.add(i);
            }
        }

        // Print and display statements
        System.out.println();
        System.out.println(al);
    }
}

Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0 1 2 3 4 5 6 7 8 9 
[1, 1, 1, 3, 3, 3, 5, 5, 5, 7, 7, 7, 9, 9, 9]


Note: Similarly, there are certain limitations with ListIterator. It is the most powerful iterator but it is only applicable for List implemented classes, so it is not a universal iterator.

Important Points

  1. Please note that initially, any iterator reference will point to the index just before the index of the first element in a collection.
  2. We don’t create objects of Enumeration, Iterator, ListIterator because they are interfaces. We use methods like elements(), iterator(), listIterator() to create objects. These methods have an anonymous Inner Class that extends respective interfaces and return this class object.

Note: The $ symbol in reference class name is a proof that concept of inner classes is used and these class objects are created. 

This can be verified by the below code. For more on inner class refer 

Java
// Java program to demonstrate iterators references

// Importing required classes from java.util package
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;

// Main class
public class GFG {

    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of Vector class
        Vector v = new Vector();

        // Creating three iterators
        Enumeration e = v.elements();
        Iterator itr = v.iterator();
        ListIterator ltr = v.listIterator();

        // Print class names of iterators
        // using getClass() and getName() methods
        System.out.println(e.getClass().getName());
        System.out.println(itr.getClass().getName());
        System.out.println(ltr.getClass().getName());
    }
}

Output
java.util.Vector$1
java.util.Vector$Itr
java.util.Vector$ListItr


Explanation

In Java, an iterator is an interface that is used to traverse through a collection of objects one by one. It is used to iterate through any collection-based data structure, including arrays, lists, sets, and maps.

An iterator has three main methods that are used to traverse through the collection:

  • hasNext() – This method checks if there is another element in the collection that can be iterated over.
  • next() – This method returns the next element in the collection.
  • remove() – This method removes the current element from the collection.
     

The Iterator interface is a part of the Java Collection Framework, and it is implemented by the classes that represent the different types of collections.

Program

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

public class IteratorExample {
    public static void main(String[] args)
    {
        ArrayList<String> names = new ArrayList<String>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add("David");

        // Creating an iterator for the names list
        Iterator<String> iterator = names.iterator();

        // Iterating over the names list using the iterator
        while (iterator.hasNext()) {
            String name = iterator.next();
            System.out.println(name);
        }
    }
}

Output
Alice
Bob
Charlie
David


In this example, we have created an ArrayList of strings and added four names to it. We have then created an iterator for the list using the iterator() method of the ArrayList class. We have used the hasNext() method to check if there are more elements in the list to be iterated over, and the next() method to get the next element in the list. We have printed each element using the System.out.println() method.

Using an iterator to traverse through a collection is a convenient and efficient way of iterating through the collection because it allows the collection to be iterated over without knowing the internal structure of the collection. It also allows for the removal of elements from the collection while iterating over it.

Advantages of Iterator in Java:

  • The Iterator is a simple and easy-to-use interface that allows us to traverse a collection without exposing its underlying implementation.
  • The Iterator is an efficient way to iterate over a collection, especially when we have a large amount of data.
  • The Iterator provides a safe way to remove elements from a collection during iteration without causing any concurrent modification exceptions.
  • The Iterator interface is implemented by all the collection classes in Java, so we can use the same code to iterate over different types of collections.

Disadvantages of Iterator in Java:

There are certain disadvantages of using Iterator in Java as mentioned below:

  • The Iterator is a unidirectional interface, meaning that we can only move forward through a collection. We cannot move backward or jump to a specific element.
  • The Iterator is not thread-safe, so we cannot use it to iterate over a collection in a multi-threaded environment without proper synchronization.
  • The Iterator does not provide any mechanism to modify elements while iterating over a collection, apart from removing elements. If we need to modify elements, we have to use other interfaces like ListIterator or a simple for loop.


Iterators in Java

A Java Cursor is an Iterator, that is used to iterate or traverse or retrieve a Collection or Stream object’s elements one by one. In this article, we will learn about Java Iterators and it’s working.

 

Similar Reads

Types of Cursors in Java

There are three cursors in Java as mentioned below:...

1. Iterator

Iterators in Java are used in the Collection framework to retrieve elements one by one. It is a universal iterator as we can apply it to any Collection object. By using Iterator, we can perform both read and remove operations. It is an improved version of Enumeration with the additional functionality of removing an element....

2. Enumeration

It is an interface used to get elements of legacy collections(Vector, Hashtable). Enumeration is the first iterator present from JDK 1.0, rests are included in JDK 1.2 with more functionality. Enumerations are also used to specify the input streams to a SequenceInputStream. We can create an Enumeration object by calling elements() method of the vector class on any vector object...

3. ListIterator

It is only applicable for List collection implemented classes like ArrayList, LinkedList, etc. It provides bi-directional iteration. ListIterator must be used when we want to enumerate elements of List. This cursor has more functionality(methods) than iterator. ListIterator object can be created by calling listIterator() method present in the List interface....