Iterator vs Enumeration:
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.
Enumeration:
Method Summary | |
---|---|
boolean | hasMoreElements() Tests if this enumeration contains more elements. |
E | nextElement() Returns the next element of this enumeration if this enumeration object has at least one more element to provide. |
Iterator:
Method Summary | |
---|---|
boolean | hasNext() Returns true if the iteration has more elements. |
E | next() Returns the next element in the iteration. |
void | remove() Removes from the underlying collection the last element returned by the iterator (optional operation). |
sources:
http://javarevisited.blogspot.com/2011/11/collection-interview-questions-answers.html#ixzz2dLkFqR00
http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html
http://docs.oracle.com/javase/6/docs/api/java/util/Enumeration.html
No comments:
Post a Comment