Java Utility Library

Java Collections - emptyListIterator() Method



The java.util.Collections.emptyListIterator() method returns a list iterator that has no elements. More precisely:

  • hasNext always returns false.
  • next and previous always throws NoSuchElementException.
  • remove and set always throws IllegalStateException.
  • add always throws UnsupportedOperationException.
  • nextIndex always returns 0.
  • previousIndex always returns -1.

Syntax

public static <T> ListIterator<T> emptyListIterator()

Here, T is the type of element, if there were any, in the iterator.


Parameters

No parameter is required.

Return Value

Returns an empty list iterator.

Exception

NA.

Example:

In the example below, the java.util.Collections.emptyListIterator() method is used to create an empty list iterator.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an empty Iterator
    ListIterator<Integer> Itr = Collections.emptyListIterator();

    //print the content of the Iterator
    while(Itr.hasNext()) {
      System.out.println(Itr.next());
    }
    System.out.println("List Iterator is empty.");
  }
}

The output of the above code will be:

List Iterator is empty.

Example:

Lets see one more example to understand the method in detail.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an empty Iterator
    ListIterator<Integer> Itr = Collections.emptyListIterator();

    //printing nextIndex and previousIndex
    System.out.println(Itr.nextIndex());
    System.out.println(Itr.previousIndex());
  }
}

The output of the above code will be:

0
-1

❮ Java.util - Collections