1.Batch分批處理
假設(shè)批處理邏輯為打印批的各個(gè)元素:
private static void process(List<String> batch) {
System.out.println(batch);
}
一般拘悦,對集合分批處理會涉及計(jì)算批次數(shù)丢习、逐批處理,如下:
/**
* 基礎(chǔ)for遍歷方式
*
* @param records
*/
private static void iterWay(List<String> records) {
List<String> batch = new ArrayList<>();
for (int index = 0, size = records.size(); index < size; index++) {
batch.add(records.get(index));
if (batch.size() == BATCH) {
process(batch);
batch.clear();
}
}
// 最后一批
if (batch.size() > 0) {
process(batch);
}
}
使用stream可以更優(yōu)雅地解決分批處理問題:
/**
* 使用stream方式
*
* @param records
*/
private static void streamWay(List<String> records){
IntStream.range(0, (records.size() + BATCH - 1) / BATCH)
.mapToObj(i -> records.subList(i * BATCH, Math.min(records.size(), (i + 1) * BATCH)))
.forEach(batch -> process(batch));
}
簡單測試一下:
public static void main(String[] args) {
List<String> records = Stream.of("a", "b", "c", "d", "e").collect(Collectors.toList());
// base way
iterWay(records);
// stream way
streamWay(records);
}