1.

Differentiate between Iterator and Enumeration.

Answer»

Iterator: Because it can be applied to any Collection object, it is a UNIVERSAL iterator. We can EXECUTE both read and remove operations using Iterator. It's an ENHANCED version of Enumeration that adds the ability to remove an element from the list.

Enumeration: An enumeration (or enum) is a data type that is defined by the user. It's mostly used to give integral constants names, which make a program EASIER to COMPREHEND and maintain. Enums are represented in Java (since 1.5) through the enum data type.

IteratorEnumeration
Iterator is a universal cursor since it works with all collection classes.Because it only applies to legacy classes, enumeration is not a universal cursor.
Iterators can make changes (for example, the delete() method removes an element from a Collection during traversal).The Enumeration interface is a read-only interface, which means you can't make any changes to the Collection while traversing its elements.
The remove() method is available in the Iterator class.The remove() method is not available in the enumeration.
Iterator is not a legacy interface. Iterator can traverse HashMaps, LinkedLists, ArrayLists, HashSets, TreeMaps, and TreeSets.Enumeration is a legacy interface for traversing Hashtables and Vectors.


Discussion

No Comment Found