• 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

iterator

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why their are iterators in java while we can access collections with their own methods???
 
Saloon Keeper
Posts: 15510
363
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because sometimes you want to inspect every value in a Collection. For some collection types, finding the next element is much faster if it remembered where the last element was.

For example, LinkedList isn't random access like ArrayList is. For an ArrayList, you can just do get(3) and BAM! There it is. You could do this in a loop for every element.
However, this is not the case for LinkedList. A LinkedList first needs to ask the zeroth element where the first element is stored; then it asks the first element where the second element is stored; etc. If you use get() in a loop for every element, the performance will be awful, because when you ask where the second element is, the List first has to find the first element again, even if it already found it before.

Iterator makes this much faster, because it remembers the last element, and asks it directly where the next element is. It also provides a standard tool for all collection classes to iterate over all their elements. This allows us to write an enhanced for loop for every collection:
We don't even care what type "names" is. If it implements Iterable, we can be sure we can iterate over all its elements reasonably fast.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:For some collection types, finding the next element is much faster if it remembered where the last element was.


For other collection types like Set, it's the only way of getting each element.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not necessarily. Every collection has a toArray() method.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Not necessarily. Every collection has a toArray() method.


Ah, but the mechanics of the two aren't the same. toArray() normally guarantees that it is "safe" (ie, a snapshot), and therefore won't show any changes subsequent to its creation.

But I completely agree with your first post, and
@maha: You should know about the RandomAccess interface. Any List that does NOT implement it (eg, LinkedList) is likely to have very bad 'for(int i = 0...' style efficiency, but its Iterator will be as fast as it can be; which is why you should generally use enhanced 'for' loops with Lists.

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well my point was more that you can access objects in any collection without using the Iterator. Not that it's a good idea :P
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Not necessarily. Every collection has a toArray() method.


You do know that several collection implementations use iterator() to fill the returned array, right? AbstractCollection does it this way (it's even documented), and nearly all Set implementations inherit the toArray methods from AbstractCollection.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you got me there, haha.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic