Whats the use of an Iterator??
3 posts in topic
Flat View  Flat View

Posted By:   suma_padaki
Posted On:   Sunday, August 22, 2004 10:22 PM

As we already have methods to retrieve elements from the Collection objects or alternatively we could loop through the Collection Objects, i just wanted to clarify the use of Iterators. Is there any advantage??
Thanks in advance............

Re: Whats the use of an Iterator??

Posted By:   Esteve_Olm  
Posted On:   Wednesday, September 15, 2004 08:19 AM

Iterators provides the same access method to all Collections. Using Iterators you don't need to know the data structure in order to loop over it. Internal data can be stored as an Arraylist, Linkedlist, JDBC Query (ResultSet) or any other Collection.

Because it's separate from the Collection itself, it also allows more than one iterator to exist at the same time, and these iterators can be implemented to filter or sort Collections against certain criteria.

An example of this may be an iterator implementation that only displays news articles based on defined preferences or one that filters corporate documents based on security level, both over the same data Collection.

Re: Whats the use of an Iterator??

Posted By:   amit_vyas  
Posted On:   Tuesday, August 24, 2004 09:38 PM

Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.

And iterator only came with new collection frame work so ppl are using with iterator wit collections.

Re: Whats the use of an Iterator??

Posted By:   Durai_kumar  
Posted On:   Monday, August 23, 2004 12:22 AM

As you said, we have methods to get one element or a collection of elements from a collection object. But how will you loop through the collection objects. There are 2 ways to loop through the collection objects. We have to either use Enumeration or iterator. Iterator is better among them. As said in the java docs,

The functionality of Enumeration interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.

Hope this helps.

About | Sitemap | Contact