Java Collections enumeration() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.enumeration(c) returns an Enumeration that walks over the elements of the collection c. Enumeration is the pre-Java-1.2 ancestor of Iterator, with the methods hasMoreElements() and nextElement().

New code should use Iterator or a for-each loop, but a few older APIs still demand an Enumeration. This method bridges any List, Set, or other collection to them.

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

Syntax

Pass the collection; loop with hasMoreElements/nextElement.

Java
Enumeration<String> e = Collections.enumeration(list);
while (e.hasMoreElements()) {
    String s = e.nextElement();
}
CallWhat it does
Collections.enumeration(c)Enumeration over c, in the collection's iteration order.
e.hasMoreElements()Like Iterator.hasNext().
e.nextElement()Like Iterator.next().
Collections.list(e)The reverse direction: Enumeration back to an ArrayList.

Available since Java 1.2. The Enumeration is backed by the collection's own iterator, so it visits elements in the same order a for-each loop would.

Library declarations
Java
// java.util.Collections
public static <T> Enumeration<T> enumeration(final Collection<T> c)

Parameters

ParameterDescription
cThe collection to enumerate. Must not be null.

Return Value

Return ValueDescription
Enumeration<T>An enumeration over the elements of c.

Compare with Similar Methods

Moving between old and new styles:

MethodHow it differs
list(e)Copies an Enumeration into a new ArrayList.
emptyEnumeration()An Enumeration with nothing in it.
c.iterator()The modern way to walk a collection.

Java Examples

Example 1 — Walk a List with an Enumeration

Number each day using the classic hasMoreElements/nextElement loop, then step past the end.

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

public class CollectionsEnumerationLoop {
    public static void main(String[] args) {
        List<String> days = Arrays.asList("Mon", "Tue", "Wed");
        Enumeration<String> e = Collections.enumeration(days);
        int n = 0;
        while (e.hasMoreElements()) {
            n++;
            System.out.println(n + ". " + e.nextElement());
        }
        System.out.println("more? " + e.hasMoreElements());
        try {
            e.nextElement();
        } catch (NoSuchElementException ex) {
            System.out.println("past the end: " + ex.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Same order as the list. The enumeration follows the list's iterator, so the days come out Mon, Tue, Wed.
  2. Loop ends cleanly. After the third element, hasMoreElements() returns false.
  3. Overrun. Asking for one more element anyway throws NoSuchElementException.

Example 2 — Join Streams with SequenceInputStream

SequenceInputStream takes an Enumeration of streams. Build the parts in a List and convert it.

Java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsEnumerationStreams {
    public static void main(String[] args) throws IOException {
        List<InputStream> parts = new ArrayList<>();
        parts.add(new ByteArrayInputStream("Hello, ".getBytes(StandardCharsets.US_ASCII)));
        parts.add(new ByteArrayInputStream("Java ".getBytes(StandardCharsets.US_ASCII)));
        parts.add(new ByteArrayInputStream("streams!".getBytes(StandardCharsets.US_ASCII)));

        SequenceInputStream all = new SequenceInputStream(Collections.enumeration(parts));
        StringBuilder text = new StringBuilder();
        int b;
        while ((b = all.read()) != -1) {
            text.append((char) b);
        }
        all.close();
        System.out.println(text);
        System.out.println("bytes read: " + text.length());
    }
}

How It Works

  1. Modern list of parts. Three in-memory streams are collected in an ordinary ArrayList.
  2. Legacy constructor. SequenceInputStream was designed before collections existed, so it wants an Enumeration; Collections.enumeration(parts) supplies it.
  3. One continuous stream. Reading byte by byte runs through all three parts in order: 7 + 5 + 8 = 20 bytes.

Example 3 — Changing the List Mid-Way, and a Round Trip

Add to the list while an enumeration is in progress, then convert a fresh enumeration back with Collections.list.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.List;

public class CollectionsEnumerationFailFast {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(Arrays.asList("a", "b", "c"));
        Enumeration<String> e = Collections.enumeration(names);
        System.out.println("first: " + e.nextElement());

        names.add("d");
        try {
            e.nextElement();
        } catch (ConcurrentModificationException ex) {
            System.out.println("after list.add: " + ex.getClass().getSimpleName());
        }

        ArrayList<String> copy = Collections.list(Collections.enumeration(names));
        System.out.println("round trip: " + copy + " equal? " + copy.equals(names));
    }
}

How It Works

  1. Started. The first nextElement() returns "a" from the underlying ArrayList iterator.
  2. Fail-fast. Adding "d" changes the list behind the iterator's back, so the next nextElement() throws ConcurrentModificationException.
  3. There and back. A new enumeration fed into Collections.list rebuilds an equal ArrayList of all four names.

Frequently Asked Questions

It returns a legacy Enumeration that walks over the elements of any Collection.
A few older APIs, such as SequenceInputStream and some servlet methods, still take or return Enumeration.
Use while (e.hasMoreElements()) { T x = e.nextElement(); ... }.
Call Collections.list(enumeration), which returns a new ArrayList.
It uses the collection's iterator, so for fail-fast collections like ArrayList, modifying the collection during enumeration usually causes ConcurrentModificationException.

Next: fill()

Replace every element of a list with one value.

fill() 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