• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Can I create 2 iterators for a single collection?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic