This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hi , The functionality of enumerator is duplicated by the Iterator . In addition, Iterator adds an optional remove operation, and has shorter method names. Iterators allow the caller to remove elements from the underlying collection during the iteration , hope this helps
SCJP, SCWD <br />A farmer learns more from a bad harvest than a good one.
use iterator when possible. Enumeration is the old class from the old collection API. iterator is the new one with simple methods names and a lot more functionality.
//*************CODE BLOCK FOR ENUMERATION******************* start=System.currentTimeMillis(); while(enum.hasMoreElements()) {
element =enum.nextElement(); } System.out.println("Enumeration took " + (System.currentTimeMillis()-start));
//*****CODE BLOCK FOR ITERATOR********************** start=System.currentTimeMillis(); while(iter.hasNext()) { element=iter.next(); } System.out.println("Iterator took " + (System.currentTimeMillis()-start)); //*************END OF ITERATOR BLOCK************************ System.gc(); //request to GC to free up some memory*/
//************END OF ENUMERATION BLOCK********************** } }
Originally posted by Monoranjan Gorai: I think it is better to use Enumeration because Iterator is musch slower than Enumeration.
That is not always true. Remember that both Enumeration and Iterator are interfaces. The speed at which they work depends on the implementation of the Enumeration and Iterator that you use.
The API documentation of Enumeration says:
NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
The API documentation of Iterator says:
An iterator over a collection. Iterator takes the place of Enumeration in the Java collections framework. Iterators differ from enumerations in two ways:
- Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. - Method names have been improved.
Another important difference is that iterators are fail fast - they throw an exception if the collection gets changed under them. Enumerations can produce all kinds of strange errors instead, which are hard to analyze.
That's why in fact you should prefer iterators, even if they are a little bit slower.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Iterators in general aren't necessarily fail-fast (it's not part of the standard contract for Iterator). But Iterators for certain commonly used classes like ArrayList and LinkedListare fail-fast. For other classes, that's not necessarily the case.
"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Jim Yingst: Iterators in general aren't necessarily fail-fast (it's not part of the standard contract for Iterator). But Iterators for certain commonly used classes like ArrayList and LinkedListare fail-fast. For other classes, that's not necessarily the case.
Good point.
I'd guess, though, that those in the standard API that are slower than their Enumeration counterpart, are slower (at least in part) *because* they are fail-fast.
So at least in those cases, being faster isn't generally a good reason to use enumerations to me.
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Yes, that did indeed appear the case when a few of us looked at that issue a couple years ago... [ April 28, 2006: Message edited by: Jim Yingst ]