Enumeration
Stay organized with collections
Save and categorize content based on your preferences.
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
.
NOTE: The functionality of this interface is duplicated by the Iterator
interface. In addition, Iterator adds an optional remove operation, and
has shorter method names. New implementations should consider using
Iterator in preference to Enumeration.
Public Method Summary
abstract
boolean
|
|
abstract
E
|
nextElement()
Returns the next element of this enumeration if this enumeration
object has at least one more element to provide.
|
Public Methods
public
abstract
boolean
hasMoreElements
()
Tests if this enumeration contains more elements.
Returns
true
if and only if this enumeration object
contains at least one more element to provide;
false
otherwise.
public
abstract
E
nextElement
()
Returns the next element of this enumeration if this enumeration
object has at least one more element to provide.
Returns
- the next element of this enumeration.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-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 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eEnumeration\u003c/code\u003e is an interface in Java that allows you to access elements of a collection one by one, such as elements in a Vector or keys/values in a Hashtable.\u003c/p\u003e\n"],["\u003cp\u003eIt provides two main methods: \u003ccode\u003ehasMoreElements()\u003c/code\u003e to check if there are more elements and \u003ccode\u003enextElement()\u003c/code\u003e to retrieve the next element.\u003c/p\u003e\n"],["\u003cp\u003eWhile functional, \u003ccode\u003eEnumeration\u003c/code\u003e is considered a legacy interface and new implementations are encouraged to utilize the \u003ccode\u003eIterator\u003c/code\u003e interface instead, which offers similar functionality with enhancements like a remove operation and shorter method names.\u003c/p\u003e\n"],["\u003cp\u003eDespite being older, \u003ccode\u003eEnumeration\u003c/code\u003e is still used in certain legacy systems and provides a basic framework for iterating over collections of objects.\u003c/p\u003e\n"]]],[],null,["# Enumeration\n\npublic interface **Enumeration** \n\n|---|---|---|\n| Known Indirect Subclasses [StringTokenizer](../../../reference/java/util/StringTokenizer.html) |----------------------------------------------------------------------|---------------------------------------------------------------------------------| | [StringTokenizer](../../../reference/java/util/StringTokenizer.html) | The string tokenizer class allows an application to break a string into tokens. | |||\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\n\nNOTE: The functionality of this interface is duplicated by the Iterator\ninterface. In addition, Iterator adds an optional remove operation, and\nhas shorter method names. New implementations should consider using\nIterator in preference to Enumeration. \n\n##### See Also\n\n- [Iterator](../../../reference/java/util/Iterator.html)\n- [SequenceInputStream](../../../reference/java/io/SequenceInputStream.html)\n- [nextElement()](../../../reference/java/util/Enumeration.html#nextElement())\n- [Hashtable](../../../reference/java/util/Hashtable.html)\n- [Hashtable.elements()](../../../reference/java/util/Hashtable.html#elements())\n- [Hashtable.keys()](../../../reference/java/util/Hashtable.html#keys())\n- [Vector](../../../reference/java/util/Vector.html)\n- [Vector.elements()](../../../reference/java/util/Vector.html#elements()) \n\n### Public Method Summary\n\n|------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract boolean | [hasMoreElements](../../../reference/java/util/Enumeration.html#hasMoreElements())() Tests if this enumeration contains more elements. |\n| abstract E | [nextElement](../../../reference/java/util/Enumeration.html#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#### public abstract boolean\n**hasMoreElements**\n()\n\nTests if this enumeration contains more elements. \n\n##### Returns\n\n- `true` if and only if this enumeration object contains at least one more element to provide; `false` otherwise. \n\n#### public abstract E\n**nextElement**\n()\n\nReturns the next element of this enumeration if this enumeration\nobject has at least one more element to provide. \n\n##### Returns\n\n- the next element of this enumeration. \n\n##### Throws\n\n| [NoSuchElementException](../../../reference/java/util/NoSuchElementException.html) | if no more elements exist. |\n|------------------------------------------------------------------------------------|----------------------------|"]]