UserInfo userInfo=userProvider.get(); OrganizeEntity organize = organizeService.getById(userInfo.getDepartmentId()); TreeMap<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMap = tzglInvestmentPlanService.selectProjectDetial(params,organize); for (String key : investmentPlanProjectMap.keySet()) { List<TzglInvestmentPlanProjectModel> projectList = investmentPlanProjectMap.get(key); for (TzglInvestmentPlanProjectModel investmentPlanProject: projectList) { investmentPlanProject.setInvestmentSubjectName(generaterSwapUtil.comSelectValues(investmentPlanProject.getInvestmentSubject())); } } //按照年份分组后,降序排序 TreeMap<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMapDescending = new TreeMap<>(Comparator.reverseOrder()); investmentPlanProjectMapDescending.putAll(investmentPlanProjectMap); return ActionResult.success(investmentPlanProjectMapDescending);优化此代码
时间: 2024-02-05 19:14:02 浏览: 67
JSP.zip_jsp userinfo_jsp增删_jsp实现增删改
可以考虑以下优化:
1. 将获取用户信息和部门信息的代码提取出来,避免在每个方法里都重复获取。
```
UserInfo userInfo = userProvider.get();
OrganizeEntity organize = organizeService.getById(userInfo.getDepartmentId());
```
2. 使用 Stream API 和 lambda 表达式来简化代码。
```
TreeMap<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMap = tzglInvestmentPlanService.selectProjectDetial(params,organize);
investmentPlanProjectMap.forEach((key, projectList) -> projectList.forEach(investmentPlanProject -> investmentPlanProject.setInvestmentSubjectName(generaterSwapUtil.comSelectValues(investmentPlanProject.getInvestmentSubject()))));
TreeMap<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMapDescending = new TreeMap<>(Comparator.reverseOrder());
investmentPlanProjectMapDescending.putAll(investmentPlanProjectMap);
return ActionResult.success(investmentPlanProjectMapDescending);
```
3. 不需要先将 `investmentPlanProjectMap` 存放到一个 TreeMap 中,再进行降序排序。可以直接使用 Stream API 进行分组和排序。
```
Map<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMap = tzglInvestmentPlanService.selectProjectDetial(params,organize);
investmentPlanProjectMap.forEach((key, projectList) -> projectList.forEach(investmentPlanProject -> investmentPlanProject.setInvestmentSubjectName(generaterSwapUtil.comSelectValues(investmentPlanProject.getInvestmentSubject()))));
Map<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMapDescending = investmentPlanProjectMap.entrySet().stream()
.sorted(Map.Entry.<String, List<TzglInvestmentPlanProjectModel>>comparingByKey().reversed())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, TreeMap::new));
return ActionResult.success(investmentPlanProjectMapDescending);
```
阅读全文