List<ResourceStandingBookVo> bookVos = new ArrayList<>(); for (OrganizationLevelVo vo : vos) { ResourceStandingBookVo standingBookVo = new ResourceStandingBookVo(); List<ResourceStandingBookVo> standingBookVos = baseResourceSuppliesMapper.selectSuppliesDate(vo); standingBookVo.setWire(standingBookVos.stream().mapToLong(ResourceStandingBookVo::getWire).sum()); standingBookVo.setTransformer(standingBookVos.stream().mapToLong(ResourceStandingBookVo::getTransformer).sum()); standingBookVo.setInsulator(standingBookVos.stream().mapToLong(ResourceStandingBookVo::getInsulator).sum()); standingBookVo.setTowerMaterial(standingBookVos.stream().mapToLong(ResourceStandingBookVo::getTowerMaterial).sum()); standingBookVo.setUnitId(vo.getId()); bookVos.add(standingBookVo); }优化代码
时间: 2024-01-08 12:02:18 浏览: 72
可以尝试使用流式编程来优化代码,将多个stream合并为一个stream,减少不必要的迭代操作,示例代码如下:
```
List<ResourceStandingBookVo> bookVos = vos.stream().map(vo -> {
ResourceStandingBookVo standingBookVo = new ResourceStandingBookVo();
List<ResourceStandingBookVo> standingBookVos = baseResourceSuppliesMapper.selectSuppliesDate(vo);
standingBookVo.setWire(standingBookVos.stream().mapToLong(ResourceStandingBookVo::getWire).sum());
standingBookVo.setTransformer(standingBookVos.stream().mapToLong(ResourceStandingBookVo::getTransformer).sum());
standingBookVo.setInsulator(standingBookVos.stream().mapToLong(ResourceStandingBookVo::getInsulator).sum());
standingBookVo.setTowerMaterial(standingBookVos.stream().mapToLong(ResourceStandingBookVo::getTowerMaterial).sum());
standingBookVo.setUnitId(vo.getId());
return standingBookVo;
}).collect(Collectors.toList());
```
另外,可以考虑对`baseResourceSuppliesMapper.selectSuppliesDate`方法进行优化,尽量避免在循环中频繁调用该方法。
阅读全文