File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Enumeration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enumeration" Watch "Enumeration" New topic


Glenny Dsilva
Ranch Hand

Joined: May 09, 2005
Posts: 42
I have this code below displaying the values in a vector using the methods (method1 and method2)
both giving the same result

I would like to know what it the advantage using enumeration interface

public class En1
public void method1()
Vector a = new Vector();
for(int i = 0;i < a.size();i++)

public void method2()
Vector a = new Vector();

for (Enumeration e = a.elements(); e.hasMoreElements()
String myString = (String) e.nextElement();
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

If you use an Enumeration it means you can iterate through any collection which can generate an enumeration of its contents, such as Hashtables.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

But of course, Enumeration is a legacy interface that you shouldn't use anymore, anyway. User Iterator instead.

[Jess in Action][AskingGoodQuestions]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Vector and Hashtable are legacy classes, just as Dictionary and the legacy interface Enumeration.

Instead of Hashtable it is recomended to use HashMap, LinkedHashMap and TreeMap (for sorted ones). Instead of Enumeration use Iterator interface.

The legacy classes have been retrofitted to support the new Interface Map. Hashtable and Vector are synchronized classes and that has cost.

The iterator offers a way to visit every item of Collections and Maps.
[ May 16, 2005: Message edited by: Edwin Dalorzo ]
Monoranjan Gorai

Joined: Jul 25, 2005
Posts: 19
Can anybody tell me what extra facility does Iterator provides except remove() option and checking for the updation in the underling Collection. I find Enumeration is more efficient than 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: 15101

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:
subject: Enumeration
It's not a secret anymore!