Wednesday, September 4, 2024

What is difference between Enumeration and Iterator in Java? Answer

Though both Iterator and Enumeration allows you to traverse over elements of Collections in Java, there is some significant difference between them e.g. Iterator also allows you to remove elements from the collection during traversal but Enumeration doesn't allow that, it doesn't get remove() method. Enumeration is also a legacy class and not all Collection supports it e.g. Vector supports Enumeration but ArrayList doesn't. On the other hand, Iterator is the standard class for iteration and traversal. By the way,  what is the difference between Enumeration and Iterator in Java? 

This question is from the early ages of  Java interview, I have not seen this question in recent interviews but it was quite common during the 2000 to 2007 period, nowadays questions like the implementation of HashMap or ConcurrentHashMap, etc has taken their place.

Nonetheless, it's very important to know the fundamental difference between Iterator and Enumeration. Sometimes it's also asked as Iterator vs Enumeration or Enumeration vs Iterator which is the same. 

An important point to note is that both Iterator and Enumeration provide a way to traverse or navigate through the entire collection in Java.

And, If you are a beginner in Java Programming then you can also check out these best Java Programming online courses to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.


Iterator vs Enumeration

Between Enumeration and Iterator, Enumeration is older, and it's there from JDK1.0, while iterator was introduced later. Iterator can be used with ArrayList, HashSet, and other collection classes.  

Another similarity between Iterator and Enumeration in Java is that the functionality of the Enumeration interface is duplicated by the Iterator interface.



The only major difference between Enumeration and iterator is Iterator has a remove() method while Enumeration doesn't. 

Enumeration acts as a Read-only interface because it has the methods only to traverse and fetch the objects, whereas by using Iterator we can manipulate the objects by adding and removing the objects from collection e.g. ArrayList.

Also, the Iterator is more secure and safe as compared to Enumeration because it does not allow other threads to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException

This is by far the most important fact for me for deciding between Iterator vs Enumeration in Java.

In Summary, both Enumeration and Iterator will give successive elements, but Iterator is the new and improved version where method names are shorter and has a new method called remove. Here is a short comparison:

Enumeration
  • hasMoreElement()
  • nextElement()
  • N/A


Iterator
  • hasNext()
  • next()
  • remove()

So Enumeration is used whenever we want to make Collection objects Read-only.

Here is also a nice table with difference of Iterator and Enumeration explained



difference between iterator and enumeration in javaThat's all about difference between Enumeration and Iterator in Java. The most critical difference is that Enumeration is old and not preferred and it also doesn't have remove() method which Iterator has. It means using Iterator also allows you to remove elements during iteration. 

If you are looking for some quality interview questions you can also check

24 comments :

Javin @ Tibco RV Tutorial said...

Thanks Jeune. you liked the post this is common interview question in java and I wanted to keep it short and simple.

bhabani pattanayak said...

Hey nice1,
B in touch so that we can discuss some java things,
Bcoz u hav lots of working experience(7years) that can help me a lot,

Anonymous said...

This same interview question "What is difference between Iterator and Enumeration is asked to one of my friend Rashmi which has appeared in a java interview. thanks dude.

Javin @ tibco tutorials for beginners said...

hi Anonymous Iterator vs Enumeration is indeed a common question glad you find my blog post about iterator and enumeration useful.

Anonymous said...

In terms of performance which one is better between Enumeration and Iterator that could be the real big difference between Iterator and Enumeration. my guess is on Iterator because its new one and Enumeration is older one.

Anonymous said...

Can you please explain What is difference between HashMap iterator and ArrayList Iterator , thanks ?

Rajiv Goel said...

@Anonymous, your Guess is write Iterator has better performance than Enumeration and use of Iterator is recommended over Enumeration in Java.

Anonymous said...

Iterator is synchronized and enumeration is not.

Still Iterator vs Enumeration ..... said...

Iterator vs Enumeration is very old concept, I am not sure why people still ask Enumeration vs iterator, difference between iterator and enumeration, these questions doesn't make any sense of world of advanced Java and highly concurrent and scalable system's world.

Anonymous said...

I really enjoy reading your blogs. Your lets me dive deeply into the concepts but in very easy way.

Vijay R said...

Hi Paul, I have a requirement where i need to traverse through the Map and change the value objects in it on some condition.... but i found concurrentmodification exception..... how do i achieve to satisfy my req....

Javin @ iterate map in java said...

Hi Vijay which kind of Map are you using ? You need to make sure no thread modify Map while you are iterating over it, if its not a ConcurrentHashMap and only use Iterator.remove() method for removing items from Map.

Anonymous said...

Sir, i have a question, in your blog, you are asking that "Also Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException." And with this point, you decide whether you are going to use Iterator or Enumeration, but as you mention that with Enumeration we can not change the contents of collection. So there is no comparison between two on this point, because Enumeration has no such functionality to update the contents.

Anonymous said...

One important point to note about Iterator's remove method is that it will throw IllegalStateException if you call remove() without calling next() method of Iterator. Also you can only call remove() method one time in one iteration. Things gets more tricky when you are using ListIterator where call to remove can only be add after calling next() or prev() and without any call to add() or remove method.

Anonymous said...

One more diff is that next() in Iterator doesnt need any casting where as nextElement() in Enumeration returns Object and hence casting is required.

Anonymous said...

is iterator synchronized?

just_a_guy said...

ConcurrentModificationException has nothing to do with thread safety according to a stackoverflow reply I recently read...

Anonymous said...

Hi javin... i like reading your blogs.. but i have little doubt here.
1. as Iterator interface can iterate on any collection with an iterator method which returns us Iterator type object.
do we have any method for Enumeration Interface.????
or we can traverse through only vector which has element method.???? i'm confused

javin paul said...

Hello @Anonymous, Thanks for your comments. Actually iterator() method comes from java.lang.Iterable interface, all Collection classes implements this interface, which also allows them to be used as target of "foreach" loop. There is no similar interface for Enumeration, as you know its convention to return Enumeration by calling elements() method, which you see in Vector and other classes which allows you to enumerate.

Anonymous said...

You said "where as by using Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist", but if you add elements while iterating it throws "concurrentModificationException"

Unknown said...

Hi,
Great Article.
I want to ask 1 thing, you have written "Also Iterator is more secure and safe as compared to Enumeration". My question is, if enumeration do not allow modification( as read only ) & iterator does, then how the iterator is thread safe. Isn't be the enumerator is more safe to use ? Please do let me know.
Thanks in advance.

javin paul said...

@Navneet, Before each iteration Iterator checks whether a collection is modified concurrently by another thread or not using modCount and throws ConcurrentModificationException if collection is modified after iteration begins, except via Iterator methods. Enumeration doesn't do that kind of check, hence cannot detect such errors. This is why I called Iterator more safe and secure in concurrent environment than Enumeration.

Unknown said...

@Javin: Thanks a lot :)

Unknown said...

This article helped me a lot especially the thread saftey part. Thank a lot :)

Post a Comment