In Java 7 and later, Collections.emptyEnumeration() returns an Enumeration with no elements. Enumeration is the old (Java 1.0) way to step through elements, with hasMoreElements() and nextElement(). It is still used by classes such as Vector, Hashtable, and some I/O and servlet APIs.
On the empty enumeration, hasMoreElements() is always false and nextElement() always throws NoSuchElementException. Return it from a legacy-style method instead of null.
A static method of the java.util.Collections utility class, available since Java 7. Call it on the class name, as in Collections.emptyEnumeration(...), after import java.util.Collections;. You never create a Collections object.
Syntax
Call it with no arguments.
Java
Enumeration<String> e = Collections.emptyEnumeration();
boolean more = e.hasMoreElements(); // false
e.nextElement(); // throws NoSuchElementException
Call
What it does
Collections.emptyEnumeration()
An enumeration with no elements.
e.hasMoreElements()
Always false.
e.nextElement()
Throws NoSuchElementException.
Collections.list(e)
Turns any enumeration into an ArrayList (here, an empty one).
Since Java 7. For new code, prefer Iterator and emptyIterator(); use the enumeration form only when an older API requires it.
Library declarations
Java
// java.util.Collections
public static <T> Enumeration<T> emptyEnumeration()
Parameters
Parameter
Description
None
emptyEnumeration() takes no parameters.
Return Value
Return Value
Description
Enumeration<T>
An enumeration whose hasMoreElements() is always false.
Old collection, old interface.Vector.elements() returns an Enumeration over the vector, in insertion order.
Missing data. When there is no vector, the method returns Collections.<String>emptyEnumeration(); the witness keeps both branches typed as Enumeration<String>.
Callers stay simple. The same hasMoreElements() loop works for both results, and the empty one reports false immediately.
Frequently Asked Questions
An Enumeration with no elements. It was added in Java 7.