This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Iterator Vs Enumerator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterator Vs Enumerator" Watch "Iterator Vs Enumerator" New topic

Iterator Vs Enumerator

AJ sisodia

Joined: Jan 19, 2005
Posts: 16
I take it both serve the same purpose.
Enumeration interface has become obsolete but in case of HashTable enumerator would be preferred as HashTable does not directly support itertors.

i would like to know can i altogether leave enumerator and can depend on iterator to access elements one by one.
Why do we give preference to iterator over enumerator. What is the difference between the two if any.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

It's Hashtable (lower case t), not HashTable.

You should use HashMap instead of Hashtable, if possible. Hashtable is obsolete, just like interface Enumeration.

If you are stuck with Hashtable and you want to use an iterator, you can easily write an adapter class that adapts an Enumeration to an Iterator (note, this is untested code):

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
I agree. Here's the link:
subject: Iterator Vs Enumerator
Similar Threads
HashMap vs Hashtable
Tutorial - Collections
Iterator can safely remove?
Effective Java: Avoiding API pitfalls
Iterators on HashMap