本文介绍了如何删除具有特定列号和行号的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码花费的时间太长:
This code takes too long time:
List<DataLogg> selectedLogger = dataLoggRepository.findByLoggerId(loggerId.getValue());
for(int i = firstIndex-1; i < lastIndex; i++) {
try {
DataLogg dataLogg = selectedLogger.get(i);
if (dataLogg != null) {
dataLoggRepository.delete(dataLogg);
}
} catch(Exception e1) {}
}
有什么办法可以避免此for循环,并且仍然删除列值loggerId.getValue()
的firstIndex-1
和lastIndex
之间的所有内容?
Is there any way I can avoid this for-loop, and still delete everything between firstIndex-1
and lastIndex
that have the column value loggerId.getValue()
?
来自以下评论的建议:
List<DataLogg> selectedLogger = dataLoggRepository.findByLoggerId(loggerId.getValue());
List<DataLogg> deleteThese = new ArrayList<DataLogg>();
for(int i = firstIndex-1; i < lastIndex; i++) {
try {
DataLogg dataLogg = selectedLogger.get(i);
deleteThese.add(dataLogg);
} catch(Exception e1) {}
}
dataLoggRepository.deleteInBatch(deleteThese);
推荐答案
另一种编写方式.
List<DataLogg> selectedLogger = dataLoggRepository.findByLoggerId(loggerId.getValue());
List<DataLogg> deleteThese = IntStream.range(firstIndex - 1, lastIndex).mapToObj(i -> selectedLogger.get(i)).collect(Collectors.toList());
dataLoggRepository.deleteInBatch(deleteThese);
这篇关于如何删除具有特定列号和行号的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!