• Post Reply Bookmark Topic Watch Topic
  • New Topic

iterator  RSS feed

 
mahi tuli
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why their are iterators in java while we can access collections with their own methods???
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 4
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessarily. Every collection has a toArray() method.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you got me there, haha.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!