在Java中将较大的集合(集合,数组,列表)拆分为较小的集合,并跟踪最后返回的集合
2022-09-02 05:06:55
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 方法时返回一个较小的集合,同时跟踪返回的较小集合。我怎样才能做到这一点?