Enumeration Interface in Java
Advertisements
Enumeration Interface in Java
It is one of the predefined interface and whose object is always used for retrieving the data from any legacy collection framework variable (like vector, stack, HasTable etc.) only in forward direction but not in backward direction. Like Iterator interface object, Enumeration Interface object is pointing just before the first element of any legacy collection framework variable.
The functionality of Enumeration is more or less similar to Iterator Interface but Enumeration Interface object belongs to synchronized and Iterator Interface object belong to non-synchronized.
Points to Remember
- Enumeration does not support addition, removing and replacing og elements.
- Enumeration is Synchronized.
- Using Enumeration elements of legacy collection can be access only in forward direction.
- Every legacy class contains following methods to work with enumeration and it returns enumeration object.
Methods of Enumeration Interface
- public boolean hasMoreElements(): Return true if Enumeration contains more elements otherwise returns false.
- public object nextElement(): Returns the next elements of Enumeration.
Create an object of Enumeration
Syntax
Vector v=new Vector(); Enumeration e=v.elements();
Example of Enumeration
import java.util.Arraylist; class EnumerationDemo { public static void main(String args[]) { Vector<Integer> al=new vector<Integer>(); // creating Vector v.add(10); v.add(20); v.add(30); Enumeration e=v.elements(); System.out.println("Forword Direction"); while(itr.e.hasMoreElements()) { System.out.println(e.e.nextElements()); } } }
Output
Forword Direction 10 20 30
Google Advertisment