ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is the difference between enumeration and iterator in Java?

user-image
تم إضافة السؤال من قبل Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company
تاريخ النشر: 2013/08/19
Mark Guirguis
من قبل 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
من قبل 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

المزيد من الأسئلة المماثلة