• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enumeratio or iterator

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the diff between Enumeration class and Iterator class ??
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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**********************
}
}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic