优化代码:@Override public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) { List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId); if(CollectionUtil.isEmpty(taskTaskList)){ return Collections.emptyList(); } List<ProjectCountVO> result = new ArrayList<>(); Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId)); for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) { String currentProjectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(currentProjectId); // 查询项目名称 ProjectCondition queryProjectDto = new ProjectCondition(); queryProjectDto.setCompanyId(companyId); queryProjectDto.setDesignStatusList(designStatusList); queryProjectDto.setPageSize(pageSize); queryProjectDto.setUserId(userId); ResultWrapper<QueryAllProject> queryAllProjectResult = applicationServicePlatformClient.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, queryProjectDto); QueryAllProject queryAllProject = queryAllProjectResult.getData(); projectCountVO.setName(queryAllProject.getProjectName()); List<TaskTask> taskList = projectTaskEntry.getValue(); projectCountVO.setTaskCount(taskList.size()); Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task-> Optional.ofNullable(task.getProblemNum()).orElse(0))); projectCountVO.setProblemCount(problemCount); result.add(projectCountVO); } return result; }
时间: 2023-08-03 17:04:28 浏览: 92
在Java中将List转换为String输出过程解析
5星 · 资源好评率100%
可以考虑对代码进行以下优化:
1. 将查询项目名称的逻辑提取成一个方法,避免在循环中重复查询。
2. 使用lambda表达式简化代码,如使用`stream().mapToInt().sum()`代替`stream().collect(Collectors.summingInt())`。
3. 对于空集合的判断,可以使用`Optional.ofNullable().orElse()`代替`CollectionUtil.isEmpty()`。
优化后的代码如下:
```
@Override
public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) {
List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId);
if (CollectionUtil.isEmpty(taskTaskList)) {
return Collections.emptyList();
}
List<ProjectCountVO> result = new ArrayList<>();
Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream()
.collect(Collectors.groupingBy(TaskTask::getProjectId));
for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) {
String currentProjectId = projectTaskEntry.getKey();
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(currentProjectId);
projectCountVO.setName(queryProjectName(currentProjectId)); // 查询项目名称
List<TaskTask> taskList = projectTaskEntry.getValue();
projectCountVO.setTaskCount(taskList.size());
Integer problemCount = taskList.stream()
.mapToInt(task -> Optional.ofNullable(task.getProblemNum()).orElse(0))
.sum();
projectCountVO.setProblemCount(problemCount);
result.add(projectCountVO);
}
return result;
}
/**
* 查询项目名称
*/
private String queryProjectName(String projectId) {
ProjectCondition queryProjectDto = new ProjectCondition();
queryProjectDto.setCompanyId(companyId);
queryProjectDto.setDesignStatusList(designStatusList);
queryProjectDto.setPageSize(pageSize);
queryProjectDto.setUserId(userId);
ResultWrapper<QueryAllProject> queryAllProjectResult = applicationServicePlatformClient.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, queryProjectDto);
QueryAllProject queryAllProject = queryAllProjectResult.getData();
return queryAllProject.getProjectName();
}
```
阅读全文