Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

??? Difference Between Iterator & Enumeration ???

 
AmitVijay AVKulkarni
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dear all,

If I have a Vector which has say some values taken from database. Now I want to extract those values, I can use either Enumeration or Iterator.

So can anybody tell me what is a difference these two? And which is a best to use?

~ Amit
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, Enumeration and Iterator do a similar thing but Iterator is now preferred. The Collection classes such as List implement Iterable and hence use Iterators. From the Enumeration JavaAPI docs:

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.

To be honest, you might also want to consider whether it is really essential to use a Vector, or if you can use something else like an ArrayList.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, Iterators are fail-fast. If the underlying collection is changed while iterating, Enumerations will produce undefined behaviour, whereas Iterators throw a ConcurrentModificationException.

(This probably should go into the FAQ...)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that not all Iterators are fail-fast. This behavior is guaranteed for Iterators of most of the standard implementations: ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, LinkedHashMap, LinkedHashSet. But it's not guaranteed for Iterators in general. For example most of the classes that might be returned from methods in the Collections class do not have fail-fast behavior.
 
Monoranjan Gorai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would suggest to use Enumeration insteed of Iterator. Because Enumeration is very fast compared to Iterator. See the example below:

/**
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
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monoranjan, please don't post the same question in three (or maybe even more?) topics:

http://www.coderanch.com/t/323647/java/java/enumeratio-or-iterator
http://www.coderanch.com/t/399690/java/java/Enumeration
http://www.coderanch.com/t/400354/java/java/Difference-Between-Iterator-Enumeration

If you have a question about the efficiency of Enumeration vs. Iterator, please open your own topic, otherwise we'll have three different discussions going on on the same topic and it will be difficult to follow. I already gave you an answer in one of the other two topics.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic