Java Collections emptyEnumeration() Method

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

Definition

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
CallWhat 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

ParameterDescription
NoneemptyEnumeration() takes no parameters.

Return Value

Return ValueDescription
Enumeration<T>An enumeration whose hasMoreElements() is always false.

Compare with Similar Methods

Related helpers for legacy enumerations:

MethodHow it differs
enumeration(c)An enumeration over the elements of a real collection.
list(e)Copies an enumeration into an ArrayList.
emptyIterator()The modern Iterator equivalent.
new Vector<>().elements()Also empty, but allocates a Vector first.

Java Examples

Example 1 — hasMoreElements() and nextElement()

Loop over the empty enumeration the classic way, then call nextElement() directly.

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

public class CollectionsEmptyEnumerationBasic {
    public static void main(String[] args) {
        Enumeration<String> e = Collections.emptyEnumeration();

        int count = 0;
        while (e.hasMoreElements()) {
            e.nextElement();
            count++;
        }
        System.out.println("loop ran " + count + " times");

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

How It Works

  1. Classic loop. while (e.hasMoreElements()) is the standard way to consume an Enumeration.
  2. Zero passes. The condition is false at once, so the counter stays at 0.
  3. Forcing it. Calling nextElement() anyway throws NoSuchElementException.

Example 2 — Turn the Empty Enumeration into a List You Can Fill

Collections.list copies any enumeration into a new ArrayList, which you can then add to.

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class CollectionsEmptyEnumerationList {
    public static void main(String[] args) {
        Enumeration<Integer> none = Collections.emptyEnumeration();

        ArrayList<Integer> list = Collections.list(none);
        System.out.println("copied: " + list);

        list.add(7);
        System.out.println("after add: " + list);
    }
}

How It Works

  1. Bridge to collections. Collections.list(none) reads the enumeration to the end and returns a new ArrayList.
  2. Empty copy. There was nothing to read, so the list prints [].
  3. A real list. Unlike emptyList(), the returned ArrayList is changeable, so adding 7 works.

Example 3 — A Legacy Method That Returns an Enumeration

An old-style API returns the attribute names of a session as an Enumeration. A session with no attributes returns the empty enumeration.

Java
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;

public class CollectionsEmptyEnumerationLegacy {
    static Enumeration<String> attributeNames(Vector<String> attrs) {
        return attrs == null ? Collections.<String>emptyEnumeration() : attrs.elements();
    }

    public static void main(String[] args) {
        Vector<String> attrs = new Vector<>();
        attrs.add("user");
        attrs.add("theme");

        Enumeration<String> names = attributeNames(attrs);
        while (names.hasMoreElements()) {
            System.out.println("attr " + names.nextElement());
        }
        System.out.println("no session: " + attributeNames(null).hasMoreElements());
    }
}

How It Works

  1. Old collection, old interface. Vector.elements() returns an Enumeration over the vector, in insertion order.
  2. Missing data. When there is no vector, the method returns Collections.<String>emptyEnumeration(); the witness keeps both branches typed as Enumeration<String>.
  3. 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.
hasMoreElements() returns false; nextElement() throws NoSuchElementException.
Only for older APIs that use it, such as Vector.elements(), Hashtable.keys(), SequenceInputStream, or some servlet methods.
Call Collections.list(enumeration). It returns a new ArrayList with the elements in order.
Use emptyIterator() in new code. Use emptyEnumeration() only when a method signature requires an Enumeration.

Next: emptyIterator()

Get an Iterator that has no elements (Java 7+).

emptyIterator() 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.

12 people found this page helpful