public Collection<Comment> getCommentCollection() {
commentCollection = movie.getCommentCollection();
return split((List<Comment>) commentCollection, 4);
}
public Collection<Comment> split(List<Comment> list, int size){
int numBatches = (list.size()/size) + 1;
Collection[] batches = new Collection[numBatches];
Collection<Comment> set = commentCollection;
for(int index = 0; index < numBatches; index++) {
int count = index + 1;
int fromIndex = Math.max(((count - 1) * size), 0);
int toIndex = Math.min((count * size), list.size());
batches[index] = list.subList(fromIndex, toIndex);
set = batches[index];
}
return set;
}
嗨,我試圖將集合分成更小的集合,具體取決於「母親」集合,並在每次調用get方法時返回其中一個較小的集合(記錄它是哪一個集合),有人可以幫我一個忙嗎?
非常感謝。
伊格納西奧
如何獲取子集合表單集合Java(Collections,Arrays,List)並跟蹤返回的最後一個
ArrayList的優選載體。 – 2011-03-07 16:54:11
沒有問題的直接鏈接,但你應該使用別的不是Vector(ArrayList?)=> http:// stackoverflow。com/questions/300519/arraylist-vs-vectors-in-java-if-thread-safety-isnt-a-concern – reef 2011-03-07 16:55:03
現在它是一個ArrayList。 – 2011-03-07 16:59:36