This discussion is archived
10 Replies Latest reply: Jul 9, 2012 1:05 AM by gimbal2 RSS

Why iterator

835297 Newbie
Currently Being Moderated
Why do I need an Iterator (vector.iterator() or arraylist.iterator()) to iterate a collection object though I have all required methods to access the collection object.
  • 1. Re: Why iterator
    aksarben Journeyer
    Currently Being Moderated
    Who said you have to use iterators?

    You can also use loops. Or get() statements. Or something else. Depends on what your requirements are.
  • 2. Re: Why iterator
    835297 Newbie
    Currently Being Moderated
    Hi

    Can you pls tell me a scenario when I have to go only for an Iterator??
  • 3. Re: Why iterator
    796440 Guru
    Currently Being Moderated
    aksarben wrote:
    You can also use loops. Or get() statements.
    Using an iterator does use a loop.
  • 4. Re: Why iterator
    796440 Guru
    Currently Being Moderated
    832294 wrote:
    Hi

    Can you pls tell me a scenario when I have to go only for an Iterator??
    If you're using a Set or some other Colleciton that's not a List, how would you access each element?

    Also, even if you have a List, if it's a LinkedList rather than an ArrayList, indexed iteration using get(i) is O(N^2), so it will be very slow for larger lists.

    Iterator provides a consistent, O(N) way to iterate over any Collection, without having to know or care about the implementation details.
  • 5. Re: Why iterator
    aksarben Journeyer
    Currently Being Moderated
    My point was that you can use for loops without iterators.
  • 6. Re: Why iterator
    796440 Guru
    Currently Being Moderated
    aksarben wrote:
    My point was that you can use for loops without iterators.
    Okay. I was more aiming at the OP and other newbies who might be reading. There used to be a lot of "for loop vs. iterator" questions here, as if it were either/or, when the real either or is get() vs. iterator. I assume the confusion came from the fact the one of the standard idioms for using an iterator used a while loop, while get() typically used a for loop.

    The following are all equivalent:
    // "old" idiom that newbies were most familiar with
    List list = ...;
    Iterator it = list.iterator();
    while (it.hasNext()) {
      X x = (X)it.next();
      // do stuff
    }
    
    // "old" idiom that most experienced developers I've worked with used
    List list = ...;
    for (Iterator it = list.iterator(); it.hasNext();) {
      X x = (X)it.next();
      // do stuff
    }
    
    // "new" idiom which is just syntactic sugar for the above
    List<X> list = ...;
    for (X x : list) {
      // do stuff
    }
    And the wrong ways to iterate, which are equivalent to each other, but not equivalent to the above are:
    List<X> list = ...;
    
    // wrong idiom which newbies are familiar with
    for (int i = 0; i < list.length(); i++) {
      X x = list.get(i);
      // do stuff
    }
    
    // wrong idiom which is equivalent to the above, but not common
    int i = 0;
    while (i < list.length()) {
      X x = list.get(i);
      // do stuff
    }
  • 7. Re: Why iterator
    EJP Guru
    Currently Being Moderated
    Why do I need an Iterator
    You don't. It is an option. You can use it if you wish.
    Can you pls tell me a scenario when I have to go only for an Iterator??
    You never 'have to' go only for an Iterator, unless the class that provides it doesn't provide anything else, and there are examples of that in the JDK, for example javax.naming.directory.Attribute(s).

    However using an iterator is simplest, as the code is always the same regardless of the collection type.
  • 8. Re: Why iterator
    888894 Newbie
    Currently Being Moderated
    It is really useful....but still would like to know if some one is using the wrong way of iteration..which also suffice the need of hour. What is wrong in that? Is it because of only performance?
  • 9. Re: Why iterator
    EJP Guru
    Currently Being Moderated
    What is wrong in that? Is it because of only performance?
    Partly, but not only. They are fundamentally wrong because they assume a particular storage mechanism, instead of delegating that knowledge to the iterator, which is exactly what it is for.
  • 10. Re: Why iterator
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    However using an iterator is simplest, as the code is always the same regardless of the collection type.
    Indeed, its what a colleague of mine would call a "party of recognition". Without reading any documentation you'll already know how it works, simply because you've seen it before. A vastly underestimated bit of code documentation; being able to make safe assumptions about how it works.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points