优化代码:public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) { ResultWrapper<QueryAllProjectName> resultWrapper = (ResultWrapper<QueryAllProjectName>) applicationServicePlatformClient.queryProjectName(projectId); if (resultWrapper.getData() == null) { return Collections.emptyList(); } String projectName = resultWrapper.getData().getProjectName(); 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()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); projectCountVO.setName(projectName); 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-07-16 17:14:57 浏览: 110
可以尝试以下优化:
1. 使用 Optional 类简化代码。可以使用 `Optional.ofNullable()` 来避免空指针异常,使用 `orElse()` 来指定默认值。
2. 使用 Stream API 来简化代码。可以使用 `Collectors.groupingBy()` 来对列表进行分组,使用 `Collectors.summingInt()` 来统计数量。
3. 减少方法调用次数。可以将 `resultWrapper.getData()` 的结果保存在变量中,避免多次调用。
4. 将变量名命名得更具有表现力。可以将 `taskTaskList` 改为 `tasks`,将 `projectTaskMap` 改为 `projectTasks`。
优化后的代码如下:
```
public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) {
ResultWrapper<QueryAllProjectName> resultWrapper = (ResultWrapper<QueryAllProjectName>) applicationServicePlatformClient.queryProjectName(projectId);
QueryAllProjectName projectNameData = resultWrapper.getData();
if (projectNameData == null) {
return Collections.emptyList();
}
String projectName = projectNameData.getProjectName();
List<TaskTask> tasks = this.listStatisticsTask(beginTime, endTime, forceType, projectId);
if (CollectionUtil.isEmpty(tasks)) {
return Collections.emptyList();
}
Map<String, List<TaskTask>> projectTasks = tasks.stream().collect(Collectors.groupingBy(TaskTask::getProjectId));
List<ProjectCountVO> result = new ArrayList<>();
for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTasks.entrySet()) {
String projectTaskId = projectTaskEntry.getKey();
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(projectTaskId);
projectCountVO.setName(projectName);
List<TaskTask> taskList = projectTaskEntry.getValue();
projectCountVO.setTaskCount(taskList.size());
Integer problemCount = taskList.stream()
.map(TaskTask::getProblemNum)
.filter(Objects::nonNull)
.mapToInt(Integer::intValue)
.sum();
projectCountVO.setProblemCount(problemCount);
result.add(projectCountVO);
}
return result;
}
```
阅读全文