aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enumeration" Watch "Enumeration" New topic
Author

Enumeration

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();
a.add("a");
a.add("b");
for(int i = 0;i < a.size();i++)
{
System.out.println(a.elementAt(i));
}


public void method2()
{
Vector a = new Vector();
a.add("a");
a.add("b");

for (Enumeration e = a.elements(); e.hasMoreElements()
{
String myString = (String) e.nextElement();
System.out.println(myString);
}
}
}
Paul Sturrock
Bartender

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
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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
Greenhorn

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

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

Joined: Aug 16, 2005
Posts: 14144
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Enumeration