aspose file tools*
The moose likes Java in General and the fly likes enumeratio or iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "enumeratio or iterator" Watch "enumeratio or iterator" New topic
Author

enumeratio or iterator

mustang india
Ranch Hand

Joined: Feb 05, 2002
Posts: 60
Whats the diff between Enumeration class and Iterator class ??
vivek sivakumar
Ranch Hand

Joined: Aug 09, 2001
Posts: 187
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.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
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.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
This part of Sun's Java tutorial discusses the differences a bit:
http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html
Good Luck.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Monoranjan Gorai
Greenhorn

Joined: Jul 25, 2005
Posts: 19
I think it is better to use Enumeration because Iterator is musch slower than Enumeration. See the following example:

/**
Created By: Monoranjan Gorai


*/

import java.util.*;
public class Performance
{
public static void main(String[] args)
{
Vector v=new Vector();
Object element;
Enumeration enum;
Iterator iter;
long start;

for(int i=0; i<1000000; i++)
{
v.add("New Element");
}

enum=v.elements();
iter=v.iterator();

//*************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**********************
}
}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 LinkedList are 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
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 LinkedList are 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: 18671
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enumeratio or iterator