Stay organized with collections
Save and categorize content based on your preferences.
Enumeration
public
interface
Enumeration
Known indirect subclasses
StringTokenizer |
The string tokenizer class allows an application to break a
string into tokens.
|
|
An object that implements the Enumeration interface generates a
series of elements, one at a time. Successive calls to the
nextElement
method return successive elements of the
series.
For example, to print all elements of a Vector<E>
v:
for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
System.out.println(e.nextElement());
Methods are provided to enumerate through the elements of a
vector, the keys of a hashtable, and the values in a hashtable.
Enumerations are also used to specify the input streams to a
SequenceInputStream
.
Summary
Public methods |
default
Iterator<E>
|
asIterator()
Returns an Iterator that traverses the remaining elements
covered by this enumeration.
|
abstract
boolean
|
hasMoreElements()
Tests if this enumeration contains more elements.
|
abstract
E
|
nextElement()
Returns the next element of this enumeration if this enumeration
object has at least one more element to provide.
|
Public methods
asIterator
public Iterator<E> asIterator ()
Returns an Iterator
that traverses the remaining elements
covered by this enumeration. Traversal is undefined if any methods
are called on this enumeration after the call to asIterator
.
API Note:
- This method is intended to help adapt code that produces
Enumeration
instances to code that consumes Iterator
instances. For example, the JarFile.entries()
method returns an Enumeration<JarEntry>
.
This can be turned into an Iterator
, and then the
forEachRemaining()
method can be used:
JarFile jarFile = ... ;
jarFile.entries().asIterator().forEachRemaining(entry -> { ... });
(Note that there is also a JarFile.stream()
method that returns a Stream
of entries,
which may be more convenient in some cases.)
Implementation Requirements:
- The default implementation returns an
Iterator
whose
hasNext
method calls this Enumeration's
hasMoreElements
method, whose next
method calls this Enumeration's nextElement
method, and
whose remove
method throws
UnsupportedOperationException
.
Returns |
Iterator<E> |
an Iterator representing the remaining elements of this Enumeration |
hasMoreElements
public abstract boolean hasMoreElements ()
Tests if this enumeration contains more elements.
Returns |
boolean |
true if and only if this enumeration object
contains at least one more element to provide;
false otherwise. |
nextElement
public abstract E nextElement ()
Returns the next element of this enumeration if this enumeration
object has at least one more element to provide.
Returns |
E |
the next element of this enumeration. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# Enumeration\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Methods](#pubmethods) \n\nEnumeration\n===========\n\n\n`\npublic\n\n\ninterface\nEnumeration\n`\n\n\n`\n\n\n`\n\n|----------------------------|\n| java.util.Enumeration\\\u003cE\\\u003e |\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Known indirect subclasses [StringTokenizer](/reference/java/util/StringTokenizer) |---------------------------------------------------------|---------------------------------------------------------------------------------| | [StringTokenizer](/reference/java/util/StringTokenizer) | The string tokenizer class allows an application to break a string into tokens. | |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nAn object that implements the Enumeration interface generates a\nseries of elements, one at a time. Successive calls to the\n`nextElement` method return successive elements of the\nseries.\n\n\nFor example, to print all elements of a `Vector\u003cE\u003e` *v*: \n\n```\n for (Enumeration\u003cE\u003e e = v.elements(); e.hasMoreElements();)\n System.out.println(e.nextElement());\n```\n\n\nMethods are provided to enumerate through the elements of a\nvector, the keys of a hashtable, and the values in a hashtable.\nEnumerations are also used to specify the input streams to a\n`SequenceInputStream`. \n**See also:**\n\n- [Iterator](/reference/java/util/Iterator)\n- [SequenceInputStream](/reference/java/io/SequenceInputStream)\n- [nextElement()](/reference/java/util/Enumeration#nextElement())\n- [Hashtable](/reference/java/util/Hashtable)\n- [Hashtable.elements()](/reference/java/util/Hashtable#elements())\n- [Hashtable.keys()](/reference/java/util/Hashtable#keys())\n- [Vector](/reference/java/util/Vector)\n- [Vector.elements()](/reference/java/util/Vector#elements())\n\nSummary\n-------\n\n| ### Public methods ||\n|-----------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` default `[Iterator](/reference/java/util/Iterator)`\u003cE\u003e` | ` `[asIterator](/reference/java/util/Enumeration#asIterator())`() ` Returns an [Iterator](/reference/java/util/Iterator) that traverses the remaining elements covered by this enumeration. |\n| ` abstract boolean` | ` `[hasMoreElements](/reference/java/util/Enumeration#hasMoreElements())`() ` Tests if this enumeration contains more elements. |\n| ` abstract E` | ` `[nextElement](/reference/java/util/Enumeration#nextElement())`() ` Returns the next element of this enumeration if this enumeration object has at least one more element to provide. |\n\nPublic methods\n--------------\n\n### asIterator\n\nAdded in [API level 33](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic Iterator\u003cE\u003e asIterator ()\n```\n\nReturns an [Iterator](/reference/java/util/Iterator) that traverses the remaining elements\ncovered by this enumeration. Traversal is undefined if any methods\nare called on this enumeration after the call to `asIterator`.\n\n\u003cbr /\u003e\n\n##### API Note:\n\n- This method is intended to help adapt code that produces `Enumeration` instances to code that consumes `Iterator` instances. For example, the [JarFile.entries()](/reference/java/util/jar/JarFile#entries()) method returns an `Enumeration\u003cJarEntry\u003e`. This can be turned into an `Iterator`, and then the `forEachRemaining()` method can be used: \n\n JarFile jarFile = ... ;\n jarFile.entries().asIterator().forEachRemaining(entry -\u003e { ... });\n \n(Note that there is also a [JarFile.stream()](/reference/java/util/jar/JarFile#stream()) method that returns a `Stream` of entries, which may be more convenient in some cases.) \n\n##### Implementation Requirements:\n\n- The default implementation returns an `Iterator` whose [hasNext](/reference/java/util/Iterator#hasNext()) method calls this Enumeration's `hasMoreElements` method, whose [next](/reference/java/util/Iterator#next()) method calls this Enumeration's `nextElement` method, and whose [remove](/reference/java/util/Iterator#remove()) method throws `UnsupportedOperationException`.\n\n| Returns ||\n|------------------------------------------------|----------------------------------------------------------------------------|\n| [Iterator](/reference/java/util/Iterator)`\u003cE\u003e` | an Iterator representing the remaining elements of this Enumeration \u003cbr /\u003e |\n\n### hasMoreElements\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract boolean hasMoreElements ()\n```\n\nTests if this enumeration contains more elements.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----------|------------------------------------------------------------------------------------------------------------------------|\n| `boolean` | `true` if and only if this enumeration object contains at least one more element to provide; `false` otherwise. \u003cbr /\u003e |\n\n### nextElement\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract E nextElement ()\n```\n\nReturns the next element of this enumeration if this enumeration\nobject has at least one more element to provide.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----|----------------------------------------------|\n| `E` | the next element of this enumeration. \u003cbr /\u003e |\n\n| Throws ||\n|-----------------------------------------------------------------------|----------------------------|\n| [NoSuchElementException](/reference/java/util/NoSuchElementException) | if no more elements exist. |"]]