Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enumeration

 
Glenny Dsilva
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use an Enumeration it means you can iterate through any collection which can generate an enumeration of its contents, such as Hashtables.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But of course, Enumeration is a legacy interface that you shouldn't use anymore, anyway. User Iterator instead.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15369
40
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