Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterator is better than Vector when return big amount data?  RSS feed

 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public Vector getXXX() {
return aVector;
}
Someone suggest me Iteraor.Is it better?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the maximum number of rows you expect?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!