private String getProjectIds(List<String> projectIdList ) { String projectIds = "("; for (String projectId : projectIdList) { projectIds += "," + projectId; } projectIds += ")"; return projectIds; }请优化此方法
时间: 2024-02-14 16:11:56 浏览: 68
在这个方法中,字符串 `projectIds` 被频繁地修改,这会带来性能问题,可以使用 `StringBuilder` 来代替字符串拼接。同时,可以使用 Java 8 的 `String.join()` 方法来简化代码。修改后的代码如下所示:
```
private String getProjectIds(List<String> projectIdList) {
StringBuilder sb = new StringBuilder("(");
sb.append(String.join(",", projectIdList));
sb.append(")");
return sb.toString();
}
```
这样就避免了频繁的字符串拼接操作,提高了性能。
相关问题
调用“@Resource private ApplicationServicePlatformClientProxy applicationServicePlatformClientProxy;”获取“projectName"修改下面代码:@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()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); //查询项目名称 String projectName = Optional.ofNullable(projectTaskMap.get(projectId)).map(taskList -> taskList.get(0).getName()).orElse(""); 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; }
可以将原来的“projectId”修改为另一个变量,比如“id”,然后在查询项目名称时使用该变量,代码如下:
@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 id = projectTaskEntry.getKey(); // 修改为新的变量名
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(id);
// 查询项目名称
String projectName = Optional.ofNullable(projectTaskMap.get(id)).map(taskList -> taskList.get(0).getName()).orElse("");
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;
}
优化代码:@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; }
可以考虑对代码进行以下优化:
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();
}
```
阅读全文