The three cursors that are used to access the elements from any collection one by one are Enumeration, Iterator and ListIterator. Though, Iterator and Enumerator are meant for performing the same work. Still, they are distinct from each other in a sense that Enumeration has read-only access to the elements in the collection. On the other side, Iterator can read as well as remove the elements from the collection.
The important difference between the Iterator and Enumeration is that the Enumeration can’t be applied on the collection classes it is only applicable on the legacy classes. On the other hand, the Iterator is applicable to the collection classes hence, it is referred as a universal cursor. Let us learn some more difference between Iterator and Enumeration with the help of the comparison chart shown below.
Content: Iterator Vs Enumeration
|Basis for Comparison||Iterator||Enumeration|
|Basic||Iterator is a universal cursor as it is applicable for all the collection classes.||Enumeration is not a universal cursor as it applies only to legacy classes.|
|Access||Using Iterator you can read and remove the elements in the collection.||Using Enumeration you can only read the elements in the collection.|
|Methods||public boolean hasnext();|
public objects next();
public void remove();
|public boolean hasMoreElements();
public object nextElement();
|Limitation||Iterator is a unidirectional forward access cursor.|
Iterator can not replace any element in the collection.
Iterator can not add any new element in the collection.
|Enumeration is unidirectional forward access cursor.
Enumeration support only legacy classes.
Enumeration has only read-only access to the elements in a collection.
|Overcome||To overcome the limitations of Iterator you must opt for ListIterator.||To overcome the limitations of Enumeration you must opt for Iterator.|
Definition of Iterator Interface
Iterator is an interface in the collection framework. As Iterator is applicable to all the collection classes, it is referred as a universal cursor. It is the cursor used to access the elements in the collection one by one. Using Iterator, you can retrieve the elements from the collection and if you want you can also remove the elements from the collection. The object of Iterator can be created as given below.
Iterator itr = Collc.iterator();
The variable itr is an object of Iterator. Collc is any collection object which is to be cycled or iterated by using the object (itr) of the Iterator. The iterator() is the method used to create an iterator object. There are three methods in the Iterator as shown below.
public boolean hasnext(); public Object next(); public void remove();
The first method hasnext() checks whether the collection has any elements in it or not. If the collection has the elements, it will return true else will return false. The second method next() is used to retrieve the next element in the collection. The third method remove() is used to delete the elements from the collection.
Iterator can travel in a collection only in the forward direction it can’t move back while travelling. Iterator can remove the element from the collection but does not have the capability to replace any existing element with a new element neither it can add any new element in the collection. To overcome these limitations you can go for ListIterator interface.
Definition of Enumeration Interface
Enumeration is the interface applicable to the legacy classes, and it can not be applied to any collection class. Hence, it is not a universal cursor. Enumeration retrieves the element (object) from the collection one by one. Enumeration object has read-only access to the elements of the collection. Enumeration object can not change any element from the collection. Let us see how to create enumeration object, have a look.
Enumeration er = Vect.elements();
The variable er is an object of Enumeration. The Vect is the object of the vector class that has to be traversed by the object (er) of Enumeration. The method element() is used to create an object of Enumeration. There are only two methods in the Enumeration interface as shown below.
public boolean hasMoreElements(); public nextElement();
The first method hasMoreElements() is used to check the status of the collection whether it has the elements in it or it is empty. If the collection has the elements the hasMoreElements() method return true else return false. The second method nextElement() is used to retrieve the elements from the collection one by one.
When traversing is complete the nextElement() method throws NoSuchElementException. The Enumeration object travels only in the forward direction. It can not add or remove or replace any element in the collection. To overcome these limitations of the Enumeration, you must opt for Iterator.
Key Differences Between Iterator and Enumeration in Java
- The main difference between Iterator and Enumeration is that Iterator is a universal cursor, can be used for iterating any collection object. On the other hand, the Enumeration is used for traversing object of legacy class only.
- Enumeration object has only read-only access to the elements in the collection. However, the object of Iterator can read and remove the elements from the collection.
- There are two methods of iterator one to check the status of collection and one to retrieve the elements from the collection. In addition to Enumeration methods Iterator has one more method to remove the elements from the collection.
- Limitations of Enumeration are it is forward unidirectional cursor, it has read-only access, and it can be applied to the collection classes. On the other way, Iterator can not replace or add any new element in the collection and like Enumeration its also forward unidirectional cursor.
- Limitation of Enumeration are resolved by Iterator and limitations of Iterator are resolved by ListIterator.
Nowadays, Iterator and ListItertaor cursor are used as they are the universal cursor and are much efficient than Enumeration.