Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterators Advantages

 
Subbu Mv
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can any one provide me the advatges of using Iterators to iterate teh util objects like rather using the defined methods like get(),remove() etc.. on the respective objects?

Eg: Why should we use the Iterators for iterating List,Set,HashMap/HashTable objects?

Thanks!
Subbu
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you go through all elements of a Set or Map without using Iterators? List is one of the few that allows direct access. And even then you should be careful; if you have a LinkedList then calling get(...) will always need to iterate from the start or end to that place. That's because those are the only two fixed points in a LinkedList. In this case, using size() and get(...) will work but it will be much slower than using an Iterator, especially if the LinkedList becomes very large.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another advantage is that if you use get() and size() to iterate, those are functions you have to create and perform. Iterator support is built-in already thanks to the people at Sun.

Hunter
 
Kiaamaa Liammes
Ranch Hand
Posts: 32
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterations will not lead to any sort of runtime exception like IndexOutofBounds , and access using iterators are more efficient
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With an iterator you don't need to make any assumptions about the internals of a data structure. They may come from any source. E.g. you could build an iterator that iterates over millions of entries. But you still hold only the current one and let the data structure decide how to get the next one (e.g. by holding an index and loading the specified entry or by generating the next permutation). If you allow methods like size() or get(int index) you need to know the size in advance and maybe you have to load every object or at least every object until "index".

Oh, and iterator allows you to use the enhanced for loop.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a different kind of Iterator just for fun:

Just to show that Iterators are not limited to iterating over Collections and arrays.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I want fun too :-).


Oh, it's the java.lang.Iterable interface that allows the use of the enhanced for loop.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want fun? Okay. Then rewrite my iterator to return only prime numbers!
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok .

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic