Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What is the difference between enumeration and iterator in Java?

user-image
Question ajoutée par Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company
Date de publication: 2013/08/19
Mark Guirguis
par 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
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?