File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Enumeration and Iterator

 
pengpeng lin
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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?

Thanks !
 
Vyas
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking of the same.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Vyas",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Dhaval Shah
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pengpeng lin
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks you all
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic