• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterator is better than Vector when return big amount data?

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public Vector getXXX() {
return aVector;
}
Someone suggest me Iteraor.Is it better?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iterator is just an interface, so if we are talking about performance, the question can't be answered (it would depend on the type of the iterator).

Regarding general design issues, it would make your code more flexible. For example, you could change the method to return an iterator that doesn't hold the whole data in memory at the same time, without the clients noticing.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The performance concerns are probably negligible here. (They usually are.) If you're using Java 1.5, consider returning an Iterable rather than an Iterator or any Collection. An Iterator can't be used in an enhanced for loop, while an Iterable can. But like an Iterator, and Iterable doesn't obligate you to get the whole thing in memory at once before you return any values. I.e. it offers the same advantage Ilja discussed for Iterator, except it works in an enhanced for loop. If you're not using JDK 1.5, and don't plan to in the future - what's wrong with you?!! No, seriously, the answer to this question really depends on how you intend to use the method. I would suggest that it's usally better to declare your method to return a general interface (e.g. Collection, List, Iterator, Iterable) rather than a specific implementation (e.g. Vector or ArrayList) because it's always possible that later you'll discover it's advantageous to change implementations, and by using a generic interface whereever possible you make this easy to change.
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We audit the user operations in our application.The longer the user uses, the more data will return.I think just use iterator instead of vector is not the key.These audit trails will reflect in a JTable. I think we should get just one page data.When the user want to see more,then get another page.I'm here waitting for your suggestions.
Thank you in advance.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the maximum number of rows you expect?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic