The main difference between iterator and enumeration is that it is possible to read and remove elements of a collection using an iterator while it is possible only to read the elements of a collection using an enumeration.
A computer program is a set of instruction to allow the CPU to perform a certain task. Programmer can write the program using a high-level programming language such as Java. There are mechanisms to manipulate a set of objects in Java. Two of them are iterator and enumeration.
Key Areas Covered
Enumeration, Iterator, Java
What is Iterator
Iterator is an interface in the Java Collection framework. In other words, it allows going through the elements of the collection one after the other. It is possible to traverse through elements such as lists, sets, and maps. Iterator allows the caller to eliminate the elements from the specified collection during the iteration of the elements.
In the above program, there is an ArrayList called list. The add method helps to add items to the list. Line 14, prints the items in the list. The itr is of type Iterator, and the iterator() returns the iterator to the beginning of the collection. The while loop calls the hasNext() method. It returns true if there are more elements in the collection. It returns false if there are no further elements to traverse. Inside the loop, the next() method helps to obtain every element in the collection. Likewise, itr reads and displays the elements in the collection.
In line 21, the itr removes the last element, which is three, from the collection. Finally, the remaining elements, which are one and two, display on the console.
What is Enumeration
Enumeration is an interface that allows obtaining one element at a time in a collection of objects. Enumeration is considered obsolete for new code. However, it is used by several methods defined by the legacy classes such as Vector.
In the above program, days is of type Enumeration. The dayNames is an object of Vector. The add method helps to add elements to the Vector. The while loop helps to go through the elements. The hasMoreElements() will return true if there are more elements to obtain. On the other hand, it will return false when all the elements have been enumerated. Furthermore, the nextElement() method is inside the while loop helps to return the next object in the enumeration as a generic Object reference.
Difference Between Iterator and Enumeration
Iterator is an interface in the Collection framework of Java that allows traversing through a collection. But, Enumeration is an interface consist of methods that allow enumerating the elements in a collection of objects.
It is possible to read and remove elements using Iterator, whereas it is only possible to read the elements using Enumerator.
Iterator provides hasNext(), next() and remove() methods while Enumeration provides hasMoreElements() and nextElement() methods.
Furthermore, the programmer can use ListIterator instead of Iterator, whereas the programmer can use Iterator instead of Enumeration.
In brief, both Iterator and Enumeration are interfaces in Java that helps to traverse through elements of a collection. The main difference between iterator and enumeration is that it is possible to read and remove elements of a collection using an iterator while it is possible only to read the elements of a collection using an enumeration. Moreover, the programmer can also use the ListIterator to go through the elements of a collection.