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();
}
Call
What 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)
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());
}
}
}
Output
1. Mon
2. Tue
3. Wed
more? false
past the end: NoSuchElementException
How It Works
Same order as the list. The enumeration follows the list's iterator, so the days come out Mon, Tue, Wed.
Loop ends cleanly. After the third element, hasMoreElements() returns false.
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());
}
}
Output
Hello, Java streams!
bytes read: 20
How It Works
Modern list of parts. Three in-memory streams are collected in an ordinary ArrayList.
Legacy constructor.SequenceInputStream was designed before collections existed, so it wants an Enumeration; Collections.enumeration(parts) supplies it.
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.
first: a
after list.add: ConcurrentModificationException
round trip: [a, b, c, d] equal? true
How It Works
Started. The first nextElement() returns "a" from the underlying ArrayList iterator.
Fail-fast. Adding "d" changes the list behind the iterator's back, so the next nextElement() throws ConcurrentModificationException.
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.