Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between enumeration and iterator in Java?

user-image
Question added by Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company
Date Posted: 2013/08/19
Mark Guirguis
by Mark Guirguis , IT Specialist , FAB Concepts

Looking at the Java API Specification for the Iterator interface, there is an explanation of the differences between Enumeration: Iterators differ from enumerations in two ways: Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
Method names have been improved.
The bottom line is, both Enumeration and Iterator will give successive elements, but Iteratoris improved in such a way so the method names are shorter, and has an additional remove method.
Here is a side-by-side comparison: Enumeration                                                        Iterator -----------------                                                  --------------- hasMoreElement()                                                hasNext() nextElement()                                                      next() N/A                                                                     remove() As also mentioned in the Java API Specifications, for newer programs, Iterator should be preferred over Enumeration, as "Iterator takes the place of Enumeration in the Java collections framework." (From the Iterator specifications.)

Usama Saad
by Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company

Enumeration: Introduced in JDK1.0 and Method signature is long for example : Ex: hasMoreElement() , nextElement() it's not Thread Safe and it’s read only interface.
  Iterator:   introduced later and its medtod signature is Short / descriptive Ex: hasNext() , next() and it's Thread Safe and can remove data from it by calling remove ().
for more details : http://usama-saad.blogspot.com/2013/07/what-is-difference-between-enumeration.html

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.