This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Iterators Advantages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Iterators Advantages" Watch "Iterators Advantages" New topic
Author

Iterators Advantages

Subbu Mv
Greenhorn

Joined: Nov 04, 2010
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
Kiaamaa Liammes
Ranch Hand

Joined: Oct 03, 2009
Posts: 32

Iterations will not lead to any sort of runtime exception like IndexOutofBounds , and access using iterators are more efficient


SCJP ,SCWCD
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Nov 18, 2008
Posts: 433
    
    2
Oh, I want fun too :-).


Oh, it's the java.lang.Iterable interface that allows the use of the enhanced for loop.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You want fun? Okay. Then rewrite my iterator to return only prime numbers!
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Ok .

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterators Advantages