File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enumeratio or iterator

 
mustang india
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the diff between Enumeration class and Iterator class ??
 
vivek sivakumar
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Monoranjan Gorai
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic