Enumeration Interface In Collection Framework

If we want to retrieve objects one by one from the collection, then we should go for cursors. There are three types of cursors available in Java collection framework in the form of Interface having different capabilities :

  • Enumeration Interface
  • Iterator Interface
  • ListIterator Interface

We can use Enumeration to get objects one by one from the old legacy collection like Vector, Hashtable etc. It was introduced in version 1.0 (For Legacy Collection).  We can create Enumeration object by using public Enumeration<E> elements() method of Vector class.

           

Methods Of Enumeration Interface

 

Methods Description
public boolean hasMoreElements() Tests if this enumeration contains more elements.
public E nextElement() Returns the next element of this enumeration if this enumeration object has at least one more element to provide.

   

Enumeration Example using Vector class element() method

Output:

 

 Limitation Of Enumeration

  • Enumeration concept is applicable only for legacy classes like Vector, Hashtable etc, hence it is not Universal cursor.
  • By using Enumeration we can move only forward direction and we cannot move to the backward direction, and hence Enumeration is single direction cursor.
  • By using Enumeration we can get only read access and we can not perform remove operation.
  • Note: To overcome above limitation of Enumeration , we should go for Iterator or ListIterator depending on the requirement.

  

Leave a Reply

Your email address will not be published. Required fields are marked *