group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
254 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
07 Feb 2019

What is the difference between Enumeration and Iterator in Java?

Enumeration and Iterator are two interfaces in java.util package which are used to traverse over the elements of a Collection object. Though they perform the same function i.e traversing the Collection object, there are some differences exist between them. Using Enumeration, you can only traverse the Collection object. But using Iterator, you can also remove an element while traversing the Collection

This is the one major difference between Enumeration and Iterator in java. You can say Iterator is some what advanced version of Enumeration. In this post, we will see the differences between Enumeration Vs Iterator In Java.

The Difference between Enumeration and Iterator is given as follows,

1) Introduction

Iterator interface is introduced from JDK 1.2 where as Enumeration interface is there from JDK 1.0.

2) remove() method

This is the main difference between Enumeration and Iterator interface. Enumeration only traverses the Collection object. You can’t do any modifications to Collection while traversing the Collection using Enumeration. Where as Iterator interface allows us to remove an element while traversing the Collection object. Iterator has remove() method which is not there in the Enumeration interface. 

3) Fail-Fast Vs Fail-Safe

Iterator is a fail-fast in nature. i.e it throws ConcurrentModificationException if a collection is modified while iterating other than it’s own remove() method. Where as Enumeration is fail-safe in nature. It doesn’t throw any exceptions if a collection is modified while iterating.

4) Safe And Secure

As Iterator is fail-fast in nature and doesn’t allow modification of a collection by other threads while iterating, it is considered as safe and secure than Enumeration.

5) Which One To Use

According to Java API Docs, Iterator is always preferred over the Enumeration




Archana Priyadarshini
Archana Priyadarshini
Computer Science
1mo ago

1.Remove() method :  Iterator has the remove() method while Enumeration does not have remove() method.

Enumeration interface acts as a read only interface, one can not do any modifications to Collection while traversing the elements of the Collection. 

Iterator can do modifications (e.g using remove() method it removes the element from the Collection during traversal).  

Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.

Enumeration is used for read only access while Iterator is useful to manipulate the list.

2. Addition to JDK : Enumeration is added to the jdk1.0 version while Iterator is added in jdk1.2 version.

3. 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. 

4. Legacy :  Enumeration is a legacy interface which is used for traversing Vector, Hashtable. 

Iterator is not a legacy interface. Iterator can be used for the traversal of HashMap, LinkedList,  ArrayList, HashSet, TreeMap, TreeSet . 

5. Preference :  According to Oracle docs,

The functionality of Enumeration  is duplicated by the iterator interface.Iterator adds an optional remove operation,and has shorter method names. New implementations should consider using Iterator in preference to Enumeration

Vijay Khatri
Vijay Khatri
Computer Science
1mo ago

Get your answer from here: