Monday 6 May 2013

Enumeration vs Iterator or Difference between Enumerator and Iterator.


  • Enumeration is older and its there from JDK1.0 while iterator was introduced later.  
  • 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. 
  • 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.  
  • 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.

No comments:

Post a Comment