What is the difference between `Enumeration` and `Iterator`?
The main differences between Enumeration
and Iterator
are:
- Enumeration:
- An older interface, part of the original version of Java.
- Methods:
hasMoreElements()
,nextElement()
. - Does not support removing elements while iterating.
- Iterator:
- A more modern interface, part of the
java.util
package. - Methods:
hasNext()
,next()
,remove()
. - Supports element removal via the
remove()
method while iterating.
- A more modern interface, part of the
In summary, Iterator
is more flexible, supporting element removal, while Enumeration
is more limited and primarily used for legacy code.