difference between enumeration and iterator code example
Example 1: difference between iterator and listiterator
Iterator vs ListIterator
1) The Iterator traverses the elements in the forward direction only.
ListIterator traverses the elements in backward and forward directions both.
2) The Iterator can be used in List, Set, and Queue.
ListIterator can be used in List only.
3) The Iterator can only perform remove operation while traversing the
collection.
ListIterator can perform add, remove, and set operation while traversing
the collection.
Example 2: iterator vs enumerator
Iterators allow the caller to remove elements from the underlying collection
during the iteration with its remove() method. You cannot add/remove elements
from a collection when using enumerator.
Enumeration is available in legacy classes i.e Vector/Stack etc.
Iterator is available in all modern collection classes.
Another minor difference is that Iterator has improved method names e.g.
Enumeration.hasMoreElement() has become Iterator.hasNext(),
Enumeration.nextElement() has become Iterator.next() etc.
Example 3: difference between iterator vs enumerator
Iterators allow the caller to remove elements from the underlying collection
during the iteration with its remove() method. You cannot add/remove elements
from a collection when using enumerator.
Enumeration is available in legacy classes i.e Vector/Stack etc.
Iterator is available in all modern collection classes.
Another minor difference is that Iterator has improved method names e.g.
Enumeration.hasMoreElement() has become Iterator.hasNext(),
Enumeration.nextElement() has become Iterator.next() etc