Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterator or Enumeration

 
Ed Bicker
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Java In General (Intermediate). This forum is specifically for questions about this site, not Java.
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Reinl
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic