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 Programmer Certification (SCJP/OCPJP) and the fly likes Collection API 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection API" Watch "Collection API" New topic
Author

Collection API

ravichandra Nibbaragandlla
Greenhorn

Joined: May 31, 2007
Posts: 3
hai friends
can anyone help me understanding the difference between Enumuration and Iterator?

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Enumeration and Iterator gives you almost same facility.

What Ivor Horton author of Beginning Java 2 says in his book regarding that:


An enumerator provides essentially the same capability as an iterator, but it
is recommended in the Java documentation that you use an iterator in
preference to an enumerator for collections. There is nothing particularly
wrong with enumerator- it's just that Iterator<> interface declares and
optional remove() method that the Enumeration<> interface does not, and the
methods in the Iterator<> interface have shorter names than those in the Enumeration<> interface, so code that uses them will be less cluttered.



Thanks,


cmbhatt
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
Enumeration and Iterator are both interfaces.
Enumeration is a legacy interface and is more
generalized, you may find it anywhere in the
entire api. Though it is commonly used with
the legacy classes like the Vector and Hashtable
it is also used independent of them also. eg.
StringTokenizer.
Iterator on the other hand is always used with
a Collection, it always has an underlying Collection
on which it operates. Also Iterator has an additional
functionality which allows removal of element from
the underlying Collection.


The Zen of Java Programming.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you really, really need an Enumeration for a Collection you can use the static method Collections.enumerator to give you one. The Enumeration is dead, long live Enumerations!


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection API