Java Collections emptyIterator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java 7 and later, Collections.emptyIterator() returns an Iterator over nothing. Its hasNext() always returns false, and next() always throws NoSuchElementException.

It is handy when you write your own Iterable or iterator-returning method and have no data: return the empty iterator instead of null, and a for-each loop over your object simply does nothing.

A static method of the java.util.Collections utility class, available since Java 7. Call it on the class name, as in Collections.emptyIterator(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Call it with no arguments; the element type is inferred.

Java
Iterator<String> it = Collections.emptyIterator();
boolean more = it.hasNext();    // false
it.next();                      // throws NoSuchElementException
CallWhat it does
Collections.emptyIterator()An iterator with no elements.
it.hasNext()Always false.
it.next()Always throws NoSuchElementException.
it.remove()Throws IllegalStateException: nothing was returned to remove.

Since Java 7. Before that, code often wrote Collections.<T>emptyList().iterator(), which works but reads less directly.

Library declarations
Java
// java.util.Collections
public static <T> Iterator<T> emptyIterator()

Parameters

ParameterDescription
NoneemptyIterator() takes no parameters.

Return Value

Return ValueDescription
Iterator<T>An iterator whose hasNext() is always false.

Compare with Similar Methods

Related empty iteration helpers:

MethodHow it differs
emptyListIterator()Adds hasPrevious, nextIndex, and other list-iterator methods.
emptyEnumeration()The legacy Enumeration equivalent.
emptyList()A whole empty collection rather than just an iterator.
list.iterator()An iterator over a real list.

Java Examples

Example 1 — hasNext() Is false, next() Throws

Check the empty iterator, then call next() anyway.

Java
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class CollectionsEmptyIteratorBasic {
    public static void main(String[] args) {
        Iterator<Integer> it = Collections.emptyIterator();
        System.out.println("hasNext: " + it.hasNext());

        try {
            it.next();
        } catch (NoSuchElementException e) {
            System.out.println("next: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Nothing ahead. The iterator is empty from the start, so hasNext() is false.
  2. Ignoring the answer. Calling next() without checking breaks the iterator contract and throws NoSuchElementException.
  3. Same as an exhausted iterator. This is exactly how a normal iterator behaves after its last element has been returned.

Example 2 — An Iterable Folder That May Be Empty

A small Folder class can be used in a for-each loop. When it has no file list, its iterator() returns the empty iterator.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class CollectionsEmptyIteratorFolder {
    static class Folder implements Iterable<String> {
        private final List<String> files;

        Folder(List<String> files) {
            this.files = files;
        }

        @Override
        public Iterator<String> iterator() {
            return files == null ? Collections.<String>emptyIterator() : files.iterator();
        }
    }

    public static void main(String[] args) {
        for (Folder f : Arrays.asList(new Folder(Arrays.asList("a.txt", "b.txt")), new Folder(null))) {
            int count = 0;
            for (String name : f) {
                System.out.println("  " + name);
                count++;
            }
            System.out.println("files: " + count);
        }
    }
}

How It Works

  1. for-each needs an iterator. Because Folder implements Iterable, the loop calls iterator() and then hasNext() and next() for you.
  2. Folder with files. The first folder returns its list's iterator, so both names print.
  3. Folder without a list. The second folder's list is null, so it returns Collections.<String>emptyIterator(); the type witness keeps the conditional expression typed as Iterator<String>.
  4. No crash. The loop body never runs for the empty folder, and the count stays 0.

Example 3 — remove() Without next(), and forEachRemaining

Calling remove() first is illegal; forEachRemaining on an empty iterator does nothing.

Java
import java.util.Collections;
import java.util.Iterator;

public class CollectionsEmptyIteratorRemove {
    public static void main(String[] args) {
        Iterator<String> it = Collections.emptyIterator();

        try {
            it.remove();
        } catch (IllegalStateException e) {
            System.out.println("remove: " + e.getClass().getSimpleName());
        }

        int[] seen = {0};
        it.forEachRemaining(s -> seen[0]++);
        System.out.println("forEachRemaining visited " + seen[0]);
    }
}

How It Works

  1. Nothing to remove. remove() deletes the element last returned by next(); none was returned, so it throws IllegalStateException.
  2. A counter in a lambda. Local variables used in a lambda must be effectively final, so a one-element array holds the count.
  3. Zero visits. forEachRemaining (Java 8) runs the action for each remaining element, and there are none.

Frequently Asked Questions

An Iterator with no elements. hasNext() is always false. It was added in Java 7.
It throws NoSuchElementException.
It throws IllegalStateException, because next() never returned an element.
In your own Iterable class or any method that returns an Iterator, when there is no data. Returning it instead of null lets for-each loops work.
They behave the same for reading. emptyIterator() states the intent more directly and needs Java 7+.

Next: emptyList()

Get an immutable, shared empty List.

emptyList() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful