File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Difference between Enumeration and Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between Enumeration and Iterator" Watch "Difference between Enumeration and Iterator" New topic

Difference between Enumeration and Iterator

pengpeng lin

Joined: Dec 24, 2005
Posts: 26
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 !

Joined: May 21, 2007
Posts: 17
Thinking of the same.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865


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.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
Dhaval Shah
Ranch Hand

Joined: Jun 10, 2005
Posts: 56
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
pengpeng lin

Joined: Dec 24, 2005
Posts: 26
thanks you all
I agree. Here's the link:
subject: Difference between Enumeration and Iterator
It's not a secret anymore!