stream流中sorted(Comparator.reverseOrder())
时间: 2023-07-21 08:03:28 浏览: 34
stream流中的`sorted(Comparator.reverseOrder())`是用于对流中的元素进行逆序排序。它会根据提供的比较器`Comparator.reverseOrder()`来决定元素的顺序。`Comparator.reverseOrder()`是一个静态方法,它返回一个比较器,用于对元素进行逆序排序。当你在流中调用`sorted(Comparator.reverseOrder())`时,元素将按照逆序排列。
相关问题
List<Map<String, Object>> collect = hb.stream().sorted(Comparator.comparing(m -> m.get("own").toString(), Comparator.reverseOrder())).sorted(Comparator.comparing(m -> m.get("score").toString(), Comparator.reverseOrder())).collect(Collectors.toList()); 为什么这个排序score最高的排在的下面,颠倒过来排序也没用
这个排序是先按照 "own" 字段进行降序排序,然后再按照 "score" 字段进行降序排序,因此得到的结果是 "own" 字段相同的记录按照 "score" 字段降序排列。如果想要得到 "score" 字段最高的记录排在最前面,应该将两个排序的顺序颠倒,先按照 "score" 字段进行降序排序,然后再按照 "own" 字段进行降序排序。
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);优化此代码
可以考虑以下优化:
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);
```
阅读全文