我自己写了一个实用程序来将列表分成给定大小的批次.我只是想知道是否已经有任何apache commons util.
public static <T> List<List<T>> getBatches(List<T> collection,int batchSize){
int i = 0;
List<List<T>> batches = new ArrayList<List<T>>();
while(i<collection.size()){
int nextInc = Math.min(collection.size()-i,batchSize);
List<T> batch = collection.subList(i,i+nextInc);
batches.add(batch);
i = i + nextInc;
}
return batches;
}
Run Code Online (Sandbox Code Playgroud)
如果已有相同的实用程序,请告诉我.
如何在Java 8 Stream上实现"分区"操作?通过分区,我的意思是,将流划分为给定大小的子流.不知何故,它将与Guava Iterators.partition()方法完全相同,只是希望分区是懒惰评估的Streams而不是List的.
我想要一些类似于scala分组函数的东西.基本上,一次挑选2个元素并处理它们.以下是相同的参考:
Lambdas确实提供了诸如groupingBy和partitioningBy之类的东西,但它们似乎都没有像Scala中的分组函数那样做.任何指针将不胜感激.