What Are Enumeration and Iterator

What is difference between Enumeration and Iterator?
 
Enumeration Iterator
1) It is legacy interface and introduced in 1.0 version. It is non-legacy and introduced in 1.2 version.
2) Applicable only for legacy classes and it is not universal cursor. Applicable for any Collection implemented class object.
3) While iterating the elements we are not allowed to remove the objects just we can perform only read operation. While iterating we can perform removal also in addition to read operation.
4) By using elements() method we can get Enumeration object. By using iterator() method we can get Iterator object.

What are limitations of Enumeration? 

  • While iterating the elements we are not allowed to perform removal operation.
  • It is applicable only for legacy classes and it is not a universal cursor.
  • It can retrieve the elements only in forward direction.


What is difference between enum and Enumeration?

An enum can be used to define a group of named constants.  It has  introduced in 1.5 version
Ex
Class Beer{
                KO,KF,RC,FO
}

Enumeration is cursor to retrieve Objects one by one from Collection objects.
 

What is difference between Iterator and ListIterator?

  • ListIterator is the child interface of the Iterator
  • Iterator is the single direction cursor where as ListIterator is bidirectional cursor.
  • While iterating the elements by Iterator we can perform only read and remove operations. But by using ListIterator we can perform read,removal, replace and addition of new objects also.
  • Iterator is applicable for every Collecton implemented class object but ListIterator  is applicable only for List implemented class objects.
  • Iterator can be get by using iterator() of Collection interface where as ListIterator can be get by using listIterator() method of List interface
  • both are introduced in 1.2 version


What is relation between ListIterator and Iterator?
 
ListIterator is child interface of Iterator 

Java Programming

See also
Object Arrays and Collections

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.