What is the difference between Iterator and Enumeration in Java?

Pragya Keshap answered on February 4, 2023 Popularity 5/10 Helpfulness 1/10

Contents


More Related Answers


What is the difference between Iterator and Enumeration in Java?

0
Tip Pragya Keshap 1 GREPCC

Main differences between Iterator and Enumeration in Java are:

1. Version: Enumeration interface is in Java since JDK 1.0.

Iterator interface was introduced in Java 1.2.

2. remove() method: The main difference between

Enumeration and Iterator interface is remove() method.

Enumeration can just traverse a Collection object. If we

use Enumeration, we cannot do any modifications to a

Collection while traversing the collection. Iterator

interface provides remove() method to remove an element

while traversing the Collection. There is not remove()

method in Enumeration interface.

3. Method names: Names of methods in Iterator interface are

hasNext(), next(), remove(). Names of methods in

Enumeration interface are hasMoreElements(),

nextElement().

4. Legacy Interface: Enumeration is considered as a legacy

interface. It is used to traverse legacy classes like Vector,

Stack and HashTable. Iterator is a newer interface that is

used to traverse almost all of the classes in Java

Collections framework.

5. Fail-fast vs. Fail-safe: Iterator is based on fail-fast

principle. It throws ConcurrentModificationException if a

collection is modified during iteration over that collection.

An Enumeration is based on fail-safe principle. It doesn’t

throw any exception if a collection is modified during

traversal.

6. Safety: Since Iterator is fail-fast and does not allow

modification of a collection by other threads, it is

considered safer than Enumeration

https://www.geeksforgeeks.org/difference-between-iterator-and-enumeration-in-java-with-examples/

Popularity 5/10 Helpfulness 1/10 Language java
Source: Grepper
Link to this answer
Share Copy Link
Contributed on Feb 04 2023
Pragya Keshap
0 Answers  Avg Quality 2/10


X

Sign in with Google

By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
X
Grepper Account Login Required

Oops, You will need to install Grepper and log-in to perform this action.