Example & Tutorial understanding programming in easy ways.

What is the difference between Iterator and Enumeration ?

The difference between Iterator and Enumeration are following:-


                                        Iterator                           Enumeration
1 Iterators allow the caller to remove elements from the underlying collection during the iteration with its remove() method. 1 Using enumerator You can not add/remove elements from a collection.
 
2 Iterator is available in all modern collection classes. 2 Enumeration is available in legacy classes i.e Vector/Stack etc.
3 Iterator has improved method names e.g. Enumeration.hasMoreElement() has become Iterator.hasNext(). 3 Enumeration has Enumeration.nextElement() has become Iterator.next().
 
4 Iterator Can be abstract, final, native, static, or synchronized. 4 Enumerator behave as a read-only interface because it has a method to retrieve and traverse the object.
5 Iterator is more secure and safe as compared to Enumeration because it doesn't allow other thread to modify the collection object while some thread is iterating over it and throws.  

Read More →