This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I wonder what is the difference between Enumeration and Iterator? Both all them have hasMoreXXX and nextXXX methods.When shoud I use Enumeration and when shoud I use Iterator? Are there any difference in performance?
Enumerations can also be used for creating an object of SequenceInputStream, whereas Iterators cant be used. Also Iterator has remove() method but Enumeration does not have. As far as performance is concerned, I am not so sure but according to the SUN API Iterator should be given preference as compared to Enumeration
Thanks and Regards,<br />Dhaval Shah<br />SCJP 1.4<br />SCWCD 1.4
Enumeration is a legacy class; it is a class from Java version 1.0. In Java version 1.2, the Collections framework was added, which essentially replaced the old collection classes with new ones (for example: Vector -> ArrayList, Hashtable -> HashMap, Enumeration -> Iterator etc.).
Sun didn't remove the old classes when Java 1.2 came out, because they were afraid that too many people who used the old classes in their programs would complain that their programs wouldn't work anymore on Java 1.2 and newer. So unfortunately we are still stuck with those Java 1.0 classes.
When you write a new program now, don't use those old classes; use the new classes (so, for example ArrayList, HashMap, Iterator instead of Vector, Hashtable, Enumerator). There are some subtle differences between the way the old and the new collection classes work; you can find more info in the API documentation.