What is the difference between enumeration and iterator in java
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. 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. Iterator is an interface in the collection framework.
Table of Contents. Save Article. Improve Article. Like Article. Last Updated : 28 Jan, Recommended Articles. Article Contributed By :. Thanks for the explanation. Enumeration is fail-safe in nature. Chethan October 11, am. Nem November 14, am. Dillip June 25, pm. Leave a Reply Cancel reply Your email address will not be published.
Using Enumeration , you can only traverse the collection. Using Iterator , you can remove an element of the collection while traversing it. Enumeration is used to traverse the legacy classes like Vector , Stack and HashTable. Enumeration is used for read only access while Iterator is useful to manipulate the list. Addition to JDK : Enumeration is added to the jdk1.
Fail-fast or Fail-safe : Enumeration is fail-safe in nature. It does not throw ConcurrentModificationException if Collection is modified during the traversal. Iterator is fail-fast in nature. It throws ConcurrentModificationException if a Collection is modified while iterating other than its own remove method. I have already shared the difference between fail-fast and fail-safe iterators in java. Legacy : Enumeration is a legacy interface which is used for traversing Vector, Hashtable.
0コメント