Enumeration Interface In Java
java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable( like Stack, Vector, HashTable etc.) in a forward direction only and not in the backward direction. This interface has been superceded by an iterator.
The Enumeration Interface defines the functions by which we can enumerate the elements in a collection of elements. For new code, Enumeration is considered obsolete. However, several methods of the legacy classes such as vectors and properties, several API classes, application codes use this Enumeration interface.
- Enumeration is Synchronized.
- It does not support adding, removing, or replacing elements.
- Elements of legacy Collections can be accessed in a forward direction using Enumeration.
- Legacy classes have methods to work with enumeration and returns Enumeration objects.
public interface Enumeration<E>
Where E is the type of elements stored in a Collection.
The sub-interfaces of Enumeration interface is NamingEnumeration and implementing class is StringTokenizer.
Creating Enumeration Object
Vector ve = new Vector(); Enumeration e = v.elements();
January Febraury March April May June July August September Octobor November December
Java Enumeration Interface With SequenceInputStream
Creation Of Custom Enumeration
Creation of Java Enumeration using String Array
Methods Of Enumeration Interface
- E – type of elements
Modifier And Type
|default Iterator<E>||asIterator()||This method returns an Iterator which traverses all the remaining elements covered by this enumeration.|
|boolean||hasMoreElements()||On implementation, it returns the boolean value if there are more elements to extract or not and returns false when all the elements have been enumerated.|
|E||nextElement()||This method returns the next element of the enumeration. It throws NoSuchElementException when there are no more elements.|
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.