Jumat, 07 Februari 2014

What is difference between Enumeration and Iterator in Java?




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 collection during traversal but Enumeration doesn't allow that, it doesn't got the 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 difference between Enumeration and Iterator in Java?  This question is from early ages of  Java interview , I have not seen this question on recent interviews but it was quite common during 2000 to 2007 period, now days questions like implementation of HashMap or ConcurrentHashMap etc has taken its place, nonetheless its very important to know the 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.






Iterator vs Enumeration




difference between iterator and enumeration in javaBetween Enumeration and Iterator, Enumeration is older and its 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  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()





So Enumeration is used when ever we want to make Collection objects as Read-only.



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
































Source:http://javarevisited.blogspot.com/2010/10/what-is-difference-between-enumeration.html

Tidak ada komentar:

Posting Komentar