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
Call
What 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()
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());
}
}
}
Output
hasNext: false
next: NoSuchElementException
How It Works
Nothing ahead. The iterator is empty from the start, so hasNext() is false.
Ignoring the answer. Calling next() without checking breaks the iterator contract and throws NoSuchElementException.
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);
}
}
}
Output
a.txt
b.txt
files: 2
files: 0
How It Works
for-each needs an iterator. Because Folder implements Iterable, the loop calls iterator() and then hasNext() and next() for you.
Folder with files. The first folder returns its list's iterator, so both names print.
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>.
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]);
}
}