What are differences between Enumeration and Iterator This question is from early ages of interview , I have not seen this question on recent interviews but it was common during 2006-2007 , now days questions like implementation of HashMap, ConcurrentHashMap etc has take its place, nonetheless its very useful
to know fundamental difference between Iterator and Enumeration. Some time its also asked as Iterator vs Enumeration or Enumeration vs Iterator which is same. important point to note is that both Iterator and Enumeration provides way to traverse or navigate through entire collection in java
Between Enumeration and Iterator, Enumeration is older and its there from JDK1.0 while iterator was introduced later. Iterator can be used with Java arraylist, java hashmap keyset and with any other collection classes.
Another similarity between Iterator and Enumeration in Java is that functionality of Enumeration interface is duplicated by the Iterator interface.Only major difference between Enumeration and iterator is Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as by using Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist.
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. This is by far 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 new and improved version where method names are shorter, and has new method called remove. Here is a short comparison:
Enumeration
hasMoreElement()
nextElement()
N/A
Iterator
hasNext()
next()
remove()
23 comments:
You have collected excellent information on your blog. Great Job!
Thanks Paul , my effort is to share my experience to as many people as possible so that they can benefit from my knowledge and its a pleasure to me to share my knowledge.
Good article.
Really nice post. I always like posts like this which give you information but not very long to read.
Thanks Jeune. you liked the post this is common interview question in java and I wanted to keep it short and simple.
Thanks Gautam.
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,
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.
hi Anonymous Iterator vs Enumeration is indeed a common question glad you find my blog post about iterator and enumeration useful.
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.
Can you please explain What is difference between HashMap iterator and ArrayList Iterator , thanks ?
@Anonymous, your Guess is write Iterator has better performance than Enumeration and use of Iterator is recommended over Enumeration in Java.
Iterator is synchronized and enumeration is not.
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.
I really enjoy reading your blogs. Your lets me dive deeply into the concepts but in very easy way.
You have provided the best information. Keep going.....
Its so nice explanation ..........
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....
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.
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.
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.
One more diff is that next() in Iterator doesnt need any casting where as nextElement() in Enumeration returns Object and hence casting is required.
is iterator synchronized?
Post a Comment