我自己写了一个实用程序来将列表分成给定大小的批次.我只是想知道是否已经有任何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)
如果已有相同的实用程序,请告诉我.
我是一个ETL进程,我从Spring Data Repository中检索了很多实体.然后我使用并行流将实体映射到不同的实体.我可以使用使用者将这些新实体逐个存储在另一个存储库中,或者将它们收集到List中并将其存储在单个批量操作中.第一种是昂贵的,而后者可能超过可用的内存.
有没有一种很好的方法来收集流中的一定数量的元素(如限制),消耗该块,并继续并行处理直到所有元素都被处理?
我的程序中有一些SQL语句包含IN给定ID的-clauses.问题是在某些情况下可能会有超过1000个ID导致Oracle与ORA-01795崩溃.过多的物品.
所以我想把这个列表分成多个子列表.
示例:我有2403个ID
结果将是三个列表:
我写了一段有用的代码,但看起来很糟糕.有没有更好的解决方案来解决这个问题?也许与收藏家和分组或其他类似的东西?
我的代码:
Map<Integer, List<Long>> result = new HashMap<>();
ArrayList<Long> asList = new ArrayList<Long>(listOfIds);
IntStream.range(0, (listOfIds.size() / 1000) + 1)
.forEach(partGroup -> result.put(partGroup, asList.subList(partGroup * 1000, (partGroup * 1000) + Math.min(1000,
asList.size() - partGroup * 1000))));
Run Code Online (Sandbox Code Playgroud) 我有以下整数列表
List<Integer> arrayList = new ArrayList<Integer>();
for (int i = 0; i < 7; i++) {
arrayList.add(i);
}
Run Code Online (Sandbox Code Playgroud)
所以列表就像这样[0,1,2,3,4,5,6].我的情景是
如果我给value = 5作为参数,那么我想像这样拆分5个子列表
[0,5], [1,6] , [2], [3], [4]
Run Code Online (Sandbox Code Playgroud)
如果我给value = 4作为参数,那么我想像这样拆分4个子列表
[0,4], [1,5], [2,6] , [3]
Run Code Online (Sandbox Code Playgroud)
如果我给value = 3作为参数,那么我想像这样拆分3个子列表
[0,3,6], [1,4], [2,5]
Run Code Online (Sandbox Code Playgroud)
我已经测试了以下功能,但这不是我的需要.
public List<List<Integer>> chopped(List<Integer> list, final int splitCount) {
List<List<Integer>> parts = new ArrayList<List<Integer>>();
final int N = list.size();
for (int i = 0; i < N; i += splitCount) {
parts.add(new ArrayList<Notification>(list.subList(i, Math.min(N, i + splitCount))));
} …Run Code Online (Sandbox Code Playgroud)