• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I create 2 iterators for a single collection?  RSS feed

 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a method that returns a collection, can I use two iterators on it?
I want to use one iterator to return the total number records returned and the other iterator to display the actual content.

Or is there a better or standard way of doing this?

--------------------------------------------------------------
//create instance of ContentBuilderModel
ContentBuilderModel m = new ContentBuilderModel(con);

// return a collection
Colletion versions = m.viewPageVersions(id);

// page detail
Iterator pageDetail = versions.iterator();

// total number of versions available
Iterator pageVersions = versions.iterator();
--------------------------------------------------------------

Thanks,
Dave
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create two different variables which reference the same iterator object. This means both references point to the same element. You have no other possibility
 
Pete Harris
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave,

Couple of things here. There's no reason the code you've written won't work (ignoring typos, that is ). Iterators are safe to run in tandem as long they don't actually change the underlying collection (for example by calling iterator.remove() method).

On the other hand, is there any reason you can't call the size() method directly on the collection to get it's size? That way you would only need one iterator.

cheers, Pete
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!