jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes ??? Difference Between Iterator & Enumeration ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "??? Difference Between Iterator & Enumeration ??? " Watch "??? Difference Between Iterator & Enumeration ??? " New topic

??? Difference Between Iterator & Enumeration ???

AmitVijay AVKulkarni

Joined: Apr 26, 2005
Posts: 17

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

Joined: Apr 21, 2005
Posts: 410
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

Joined: Jul 11, 2001
Posts: 14112
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...)

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

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
Monoranjan Gorai

Joined: Jul 25, 2005
Posts: 19
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");


//*************CODE BLOCK FOR ENUMERATION*******************

element =enum.nextElement();
System.out.println("Enumeration took " + (System.currentTimeMillis()-start));

//*****CODE BLOCK FOR ITERATOR**********************
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: 15084

Monoranjan, please don't post the same question in three (or maybe even more?) topics:


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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link: http://aspose.com/file-tools
subject: ??? Difference Between Iterator & Enumeration ???
It's not a secret anymore!