This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Regarding TypeSafe Iterator  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can I use the same Iterator object to print the elements in the List twice?
Is there any way to do like that.

I know that once we traverse the collection using the Iterator object, IS IT POSSIBLE TO TRAVERSE THE SAME COLLECTION OBJECT AGAIN USING SAME Iterator object to print the elements..
**************************************************************************
// Here my doubt is: Is there a way to use the same Iterator
// to traverse the same collection twice by giving the same output.
***************************************************************************


My Code is as follows:
___________________________________________________________________________
package com.kris.tigerv.ch2;

import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Iterator;
import org.apache.log4j.Category;

public class TypeSafeListWithIterator {

public static void main(String[] args) {
List<String> list = new LinkedList<String>();
list.add("Sri");
list.add("Krishna");
list.add(new String("Prasad"));
list.add("Potluri");

Category cat = Category.getInstance(TypeSafeListWithIterator.class);
cat.info("...List Elements Before Sort...");

/* for(String s:list){
cat.info(s+" ");
} */
// Iterator
Iterator<String> iterator = list.iterator();

while(iterator.hasNext()) {
cat.info(iterator.next());

}



Collections.sort(list);
cat.info("...List Elements After Sort...");

/* for(String s: list){
cat.info(s);

} */

// Here my doubt is: Is there a way to use the same Iterator
// to traverse the same collection twice by giving the same output.

Iterator<String> iterator1 = list.iterator();
while(iterator1.hasNext()){
cat.info(iterator1.next());

}

}

}
___________________________________________________________________________

Thanks In Advance,
Kris.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can..


Prints the following:


The second set starting with 'four' is a sorted list.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can I use the same Iterator object to print the elements in the List twice?



I would say, "NO". "Iterator object is for one time use!"


M Krishnan says,
Yes, you can..



In your example, if you see carefully, you are getting two iterator
objects to traverse from the list. As said, Iterator object is for one time
use. We can use the phrase "USE AND THROW" for the iterator.

Each time you need to traverse from the List you create a new iterator, means calling list.iterator(); gives you new iterator object.



Regards,
cmbhatt
[ April 26, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is rather beyond the scope of SCJP.

However, you could play with ListIterator which is a bidirectional Iterator.
So, when you get to the end of the list you reset the ListIterator by going backwards until you get to the point where hasPrevious() returns false. Then you can go forwards again. Rather inefficient methinks, but it "solves" the stated problem.
 
Krishna Potluri
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

From your response I understood that, Iterator object can be used only once for traversing the Collection. If i need to traverse the same collection 2nd time then i need to create one more Iterator object.

Thanks for clearing my doubt.

Thanks and Regards,
Krishna.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!