• 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

Iterators Advantages

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 32
Eclipse IDE Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iterations will not lead to any sort of runtime exception like IndexOutofBounds , and access using iterators are more efficient
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok .

 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic