检查代码: 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()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); ProjectCondition projectCondition = new ProjectCondition(); projectCondition.setProjectName(projectId); //查询项目名称 String projectName = applicationServicePlatformClient.listUserProject(projectCondition.getProjectName(),); 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-08-03 11:03:58 浏览: 71
代码看起来没有明显的问题,但是建议在使用 Optional.ofNullable() 方法时,尽量不要使用 orElse() 方法,而是使用 orElseGet() 方法。这是因为 orElse() 方法会在 Optional 对象为空时无论如何都会执行,而 orElseGet() 方法只有在 Optional 对象为空时才会执行,可以提高一定的性能。修改后的代码如下:
```
Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task -> Optional.ofNullable(task.getProblemNum()).orElseGet(() -> 0)));
```
相关问题
通过接口“applicationServicePlatformClient”在下面代码中调用projectname并打印代码: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()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); //查询项目名称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; }
您可以这样调用projectname并打印代码:
```java
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()) {
projectId = projectTaskEntry.getKey();
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(projectId);
// 查询项目名称
String projectName = applicationServicePlatformClient.getProjectName(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);
}
System.out.println("projectname: " + projectId);
return result;
}
```
在for循环中,我们使用项目ID调用`applicationServicePlatformClient`的`getProjectName`方法,获取项目名称并设置到`ProjectCountVO`对象中。最后,在方法结束前打印`projectId`的值。
优化代码: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; }
可以尝试以下优化:
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;
}
```
阅读全文