Home Java Collections Methods emptyListIterator() Java Collections emptyListIterator() Method Definition In Java 7 and later, Collections.emptyListIterator() returns a ListIterator positioned in a list with no elements. A ListIterator can move in both directions and knows its index, so this one reports hasNext() and hasPrevious() as false, nextIndex() as 0, and previousIndex() as -1.
Use it when an API expects a ListIterator and you have nothing to iterate, for example a method that walks elements backwards.
A static method of the java.util.Collections utility class, available since Java 7. Call it on the class name, as in Collections.emptyListIterator(...), after import java.util.Collections;. You never create a Collections object.
Syntax Call it with no arguments.
ListIterator<String> it = Collections.emptyListIterator();
it.hasPrevious(); // false
it.nextIndex(); // 0
it.previousIndex(); // -1 Call What it does it.hasNext() / it.hasPrevious()Both false. it.nextIndex() / it.previousIndex()0 and -1. it.next() / it.previous()Throw NoSuchElementException. it.set(e) / it.add(e)IllegalStateException / UnsupportedOperationException.
Since Java 7. The index values are the same ones a real list iterator reports at the start of any list: the next element would be index 0 and the previous one index -1.
Library declarations // java.util.Collections
public static <T> ListIterator<T> emptyListIterator() Parameters Parameter Description None emptyListIterator() takes no parameters.
Return Value Return Value Description ListIterator<T>A list iterator with no elements in either direction.
Compare with Similar Methods Related iteration helpers:
Java Examples 1. Position info 2. set and add 3. Walk backwards Example 1 — Indexes and Directions of an Empty ListIterator Print every query method, then try to step backwards.
import java.util.Collections;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class CollectionsEmptyListIteratorInfo {
public static void main(String[] args) {
ListIterator<String> it = Collections.emptyListIterator();
System.out.println("hasNext " + it.hasNext() + ", hasPrevious " + it.hasPrevious());
System.out.println("nextIndex " + it.nextIndex() + ", previousIndex " + it.previousIndex());
try {
it.previous();
} catch (NoSuchElementException e) {
System.out.println("previous: " + e.getClass().getSimpleName());
}
}
} hasNext false, hasPrevious false
nextIndex 0, previousIndex -1
previous: NoSuchElementException How It Works Both directions blocked. There is nothing ahead and nothing behind.Start-of-list indexes. The next element would sit at index 0, and the previous one at -1, which does not exist.Stepping back fails. previous() throws NoSuchElementException, the mirror image of calling next() at the end.Example 2 — set() and add() Fail in Different Ways Both are change methods, but they fail for different reasons.
import java.util.Collections;
import java.util.ListIterator;
public class CollectionsEmptyListIteratorModify {
public static void main(String[] args) {
ListIterator<String> it = Collections.emptyListIterator();
try {
it.set("x");
} catch (IllegalStateException e) {
System.out.println("set: " + e.getClass().getSimpleName());
}
try {
it.add("x");
} catch (UnsupportedOperationException e) {
System.out.println("add: " + e.getClass().getSimpleName());
}
}
} set: IllegalStateException
add: UnsupportedOperationException How It Works set needs a current element. set replaces the element last returned by next or previous; there is none, so the state is illegal.add needs a growable list. add would insert at the cursor, which the empty, immutable source does not support.Read the exception type. IllegalStateException means "wrong moment"; UnsupportedOperationException means "never allowed".Example 3 — A Backward Printer That Accepts Any ListIterator A helper prints elements from a ListIterator in reverse. It works with a real list positioned at its end, and with the empty iterator.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
public class CollectionsEmptyListIteratorBack {
static String backwards(ListIterator<String> it) {
StringBuilder sb = new StringBuilder();
while (it.hasPrevious()) {
sb.append(it.previous()).append(' ');
}
return "[" + sb.toString().trim() + "]";
}
public static void main(String[] args) {
List<String> steps = Arrays.asList("wake", "eat", "code");
System.out.println(backwards(steps.listIterator(steps.size())));
System.out.println(backwards(Collections.<String>emptyListIterator()));
}
} How It Works Start at the end. steps.listIterator(steps.size()) places the cursor after the last element, so hasPrevious() is true.Walk back. Each previous() returns the element before the cursor: code, eat, wake.Empty input. With emptyListIterator(), hasPrevious() is false immediately, so the helper returns [].Frequently Asked Questions What does Collections.emptyListIterator() return? A ListIterator with no elements. It was added in Java 7.
What do nextIndex() and previousIndex() return on it? nextIndex() returns 0 and previousIndex() returns -1.
What happens if I call previous() on emptyListIterator()? It throws NoSuchElementException.
Why does set() throw IllegalStateException but add() throw UnsupportedOperationException? set needs an element returned by next or previous, so calling it now is illegal. add is never supported because the empty source cannot grow.
How is it different from emptyIterator()? emptyIterator() is forward-only. emptyListIterator() also offers hasPrevious, previous, nextIndex, previousIndex, set, and add.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion