• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enumeration

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But of course, Enumeration is a legacy interface that you shouldn't use anymore, anyway. User Iterator instead.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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**********************
}
}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Monoranjan, please don't post the same question in three (or maybe even more?) topics:

https://coderanch.com/t/323647/java/java/enumeratio-or-iterator
https://coderanch.com/t/399690/java/java/Enumeration
https://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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic