aspose file tools*
The moose likes Java in General and the fly likes Iterator or Enumeration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Iterator or Enumeration" Watch "Iterator or Enumeration" New topic
Author

Iterator or Enumeration

Ed Bicker
Greenhorn

Joined: Oct 14, 2003
Posts: 12
So what is the rule of thumb when to use an Enumeration or an Iterator to do
java.util.Enumeration e = req.getParameterNames();
while(e.hasMoreElements()) {
{
or
java.util.Iterator iterator = mc.getProcessList().iterator();
while (iterator.hasNext() ) {
}
what is the differtence between either???
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Moving this to Java In General (Intermediate). This forum is specifically for questions about this site, not Java.


GenRocket - A Test Data Generation Platform
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
Some existing classes may still return an Enumeration but if you are writing a new class then the API documentation recommends that you write an Iterator instead. Method names have been shortened in Iterator and a method signature for removing the current element from the collection has been defined. It's not a requirement, just current best practice.
Regards,
Ken
Greg Reinl
Ranch Hand

Joined: Feb 11, 2003
Posts: 45
Somewhat related to this topic, if you are working with a java.util.List, is there an advantage to using an Iterator rather than just calling the List's get(int i) method?
For example, instead of:

You could eliminate the iterator from the code:

Are there performance or other considerations that make one more or less desirable than the other?
[ May 11, 2004: Message edited by: Greg Reinl ]
[ May 11, 2004: Message edited by: Greg Reinl ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Greg Reinl:
Somewhat related to this topic, if you are working with a java.util.List, is there an advantage to using an Iterator rather than just calling the List's get(int i) method?

The performance difference depends very strongly on the List implementation. If it's an array list, using get(int) is very efficient, and the for-loop with the int counter is going to be a little better (from a put performance perspective) than creating the Iterator -- but basically just the cost of creating the Iterator object.
If, on the other hand, the List is a LinkedList -- well, then it's a totally different story. Using an Iterator will be way, way better than the int index! Tbe method get(int) on a LinkedList has to count links from the beginning of the list until it gets to the requested one -- which means that the time it takes to iterate a LinkedList with get() is proportional to the square of the number of elements in the list!
Now, performance considerations aside, even for ArrayList, using Iterator is nicer. First, it lets you remain flexible; it's easy to change to a LinkedList if needed without rewriting code if you use Iterator for everything. And the Iterator loop is (to me, anyway) clearer. Finally, even though we've all written "for (int i)" a million times, it's a complicated little bit of code, with many opportunities for typos; if you make a typo in the Iterator loop, it will almost certainly fail to compile.


[Jess in Action][AskingGoodQuestions]
Greg Reinl
Ranch Hand

Joined: Feb 11, 2003
Posts: 45
Thanks for the reply. It sounds like the Iterator is the way to go in most cases if you are using the List interface so that you keep List implementation options open.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Iterator or Enumeration
 
Similar Threads
diff betn iterator, enumeration and listIterator
Iterator vs Enumeration
Why final is not applied for Map ?
Iterator vs Enumeration
iterator vs Enumeration