优化以下代码List<OnWorkingEmployeeVO> onWorkingEmployees = employeeApiServiceClient.getEmpInfoByCorpIdAndStaffIds(param.getCorpId(), strList.getList()); if (onWorkingEmployees == null || onWorkingEmployees.size() == 0){ return PageModel.of(0, null); } List<SalaryBillListOfPermits> salaryBillListOfPermitsList = new ArrayList<>(); for (OnWorkingEmployeeVO onWorkingEmployee : onWorkingEmployees) { Integer numbs = hrmSalaryBillRecordDAO.getCountByCorpIdAndUserId(param.getCorpId(), onWorkingEmployee.getUserId()); SalaryBillListOfPermits salaryBillListOfPermits = new SalaryBillListOfPermits(); salaryBillListOfPermits.setNumbs(numbs); salaryBillListOfPermits.setDeptName(onWorkingEmployee.getMainDeptName()); salaryBillListOfPermits.setJobNumber(onWorkingEmployee.getEmpJobNumber()); salaryBillListOfPermits.setPersonId(onWorkingEmployee.getUserId()); salaryBillListOfPermits.setPersonName(onWorkingEmployee.getName()); salaryBillListOfPermitsList.add(salaryBillListOfPermits); }
时间: 2023-12-26 18:03:29 浏览: 43
可以考虑使用 Java8 中的 Stream API,将代码简化为一行:
```
List<SalaryBillListOfPermits> salaryBillListOfPermitsList = employeeApiServiceClient.getEmpInfoByCorpIdAndStaffIds(param.getCorpId(), strList.getList())
.stream()
.map(onWorkingEmployee -> {
Integer numbs = hrmSalaryBillRecordDAO.getCountByCorpIdAndUserId(param.getCorpId(), onWorkingEmployee.getUserId());
SalaryBillListOfPermits salaryBillListOfPermits = new SalaryBillListOfPermits();
salaryBillListOfPermits.setNumbs(numbs);
salaryBillListOfPermits.setDeptName(onWorkingEmployee.getMainDeptName());
salaryBillListOfPermits.setJobNumber(onWorkingEmployee.getEmpJobNumber());
salaryBillListOfPermits.setPersonId(onWorkingEmployee.getUserId());
salaryBillListOfPermits.setPersonName(onWorkingEmployee.getName());
return salaryBillListOfPermits;
})
.collect(Collectors.toList());
```
这样可以避免显式地创建循环,同时也更加简洁易读。需要注意的是,这里使用了`stream()`和`map()`方法对 List 进行了转换,最后使用`collect()`方法将转换后的结果收集到一个新的 List 中。
阅读全文