问题遇到的现象和发生背景
for循环如何提高效率
问题相关代码,请勿粘贴截图
HyBasicsMaterial hyBasicsMaterial = new HyBasicsMaterial();
//查询物料表里面的包规,得出需要多少张条形码
Long supplierCodeSum = hyBarCodeTem.getCustomerMaterialSum();
//获取物料包规
String customerMaterialCode = hyBarCodeTem.getCustomerMaterialCode();
hyBasicsMaterial.setCustomerMaterialCode(customerMaterialCode);
HyBasicsMaterial hyBasicsMaterial1 = hyBasicsMaterialService.selectHyBasicsMaterial(hyBasicsMaterial);
//包规数量
String materialSku = hyBasicsMaterial1.getMaterialSku();
List<HyBarCodeTem> batchList = new ArrayList();
long num = 0;
Long valueOf = Long.valueOf(materialSku);
//如果是整数
long l = System.currentTimeMillis();
if (supplierCodeSum % valueOf == 0) {
num = supplierCodeSum / valueOf;
for (long i = 0; i < num; i++) {
HyBarCodeTem saveItem = HyBarCodeTem.builder().build();
saveItem.setCustomerMaterialCode(hyBarCodeTem.getCustomerMaterialCode());
saveItem.setCreateTime(DateUtils.getNowDate());
saveItem.setCustomerMaterialCodeNo(materialSku);
saveItem.setIsValid("0");
batchList.add(saveItem);
}
} else {
//如果有零头
num = supplierCodeSum / valueOf + 1;
for (long i = 0; i < num; i++) {
HyBarCodeTem saveItem = HyBarCodeTem.builder().build();
saveItem.setCustomerMaterialCode(hyBarCodeTem.getCustomerMaterialCode());
saveItem.setCreateTime(DateUtils.getNowDate());
saveItem.setIsValid("0");
if (i == num - 1) {
saveItem.setCustomerMaterialCodeNo(String.valueOf(supplierCodeSum % valueOf));
} else {
saveItem.setCustomerMaterialCodeNo(materialSku);
}
batchList.add(saveItem);
}
}
List list = new ArrayList<>();
// batchList 为空不能保存
this.saveBatch(batchList);
for (HyBarCodeTem barCodeTem : batchList) {
Map<Object, Object> map = new HashMap<>();
Long id = barCodeTem.getId();
map.put("id", id);
list.add(map);
}
long l1 = System.currentTimeMillis();
System.out.println("运行时间:"+(l1 - l)+ "ms");
运行结果及报错内容
我的解答思路和尝试过的方法
200条大概要10s左右,如果多的话,时间更长,请问有啥提高效率的方法吗