我收到了employeeJobDataList数据后,就创建了
var resultList: List[List[AnytimePayEmployeeJobData]] = new ArrayList[List[AnytimePayEmployeeJobData]]
我要添加具有BATCH_THRESHOLD = 25
的数据列表的地方
这是代码段:
val upsertsFailedBatches: List[FailedBatch] = new ArrayList[FailedBatch]
val upsertsEmployeeJobDataList: List[AnytimePayEmployeeJobData] = new ArrayList[AnytimePayEmployeeJobData]
var resultList: List[List[AnytimePayEmployeeJobData]] = new ArrayList[List[AnytimePayEmployeeJobData]]
for (i <- 0 until employeeJobDataList.size()) {
upsertsEmployeeJobDataList.add(employeeJobDataList.get(i))
if (upsertsEmployeeJobDataList.size() == BATCH_THRESHOLD) {
resultList.add(upsertsEmployeeJobDataList)
upsertsEmployeeJobDataList.clear()
}
}
if (upsertsEmployeeJobDataList.size() > 0) {
resultList.add(upsertsEmployeeJobDataList)
}
虽然我要在resultList中添加25个数据的列表,但是在使用print语句检查大小时却显示了4 输出为:批处理大小:4
for (i <- 0 until resultList.size()){
println("batch size: "+ resultList.get(i).size())
}
答案 0 :(得分:3)
之所以不起作用,是因为您使用了可变集合。
for (i <- 0 until employeeJobDataList.size()) {
upsertsEmployeeJobDataList.add(employeeJobDataList.get(i))
if (upsertsEmployeeJobDataList.size() == BATCH_THRESHOLD) {
resultList.add(upsertsEmployeeJobDataList)
upsertsEmployeeJobDataList.clear()
}
}
当一批装满后,您:
因此,您有一个集合,其中如果重复size() / BATCH_THRESHOLD
次,则有一个相同的列表,并且其内容是最后size() % BATCH_THRESHOLD
个项目。
您应该创建一个新的集合,而不是clear
…但是如果您使用Scala集合,则可以用一个衬里替换整个代码:
upsertsFailedBatches.grouped(BATCH_THRESHOLD).toList