• 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

??? Difference Between Iterator & Enumeration ???

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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");
}

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic