List<SysDictDataDTO> collect = dictDataDTOS.stream().filter(x -> key.equals(x.getDictLabel())).collect(Collectors.toList())
时间: 2024-05-21 16:12:05 浏览: 19
好的,您的第一个问题是关于 Java 中的 Stream API,具体来说是关于如何使用 filter 方法过滤元素,并使用 collect 方法将过滤后的元素转换成 List 类型。代码中的 key 是一个条件,用来筛选出 DictDataDTO 中 dictLabel 属性等于 key 的元素,然后将这些元素转换为 List。
相关问题
优化这段代码:List<CompletableFuture<CallIntersectionVo>> futureList = Lists.newArrayList(); for (Map.Entry<String, List<String>> entry : intersectionResult.entrySet()) { CompletableFuture<CallIntersectionVo> future = CompletableFuture.supplyAsync(() -> { String account = entry.getKey(); List<String> personNoList = entry.getValue().stream().distinct().collect(Collectors.toList()); CallIntersectionVo vo = new CallIntersectionVo(); if (personNoList.size() >= 2) { List<PersonBasicVo> personVoList = Lists.newArrayList(); int count = 0; for (String personNo : personNoList) { Map<String, Object> callMap = callMapList.stream().filter(map -> personNo.equals(map.get("personNo"))).findAny().get(); List<CallRecord> callList = (List<CallRecord>) callMap.get("callList"); // 统计通话频率 count += callList.stream().filter(x -> account.equals(x.getRelationshipAccount())).count(); // 获取涉案人 personVoList.add(personList.stream().filter(person -> personNo.equals(person.getPersonNo())).findAny().get()); } // 共同号码是否属于涉案人 String commonPersonName = getCommonPersonName(personList, account); if (frequency != null && frequency > 0) { if (count >= frequency) { vo.setPersons(personVoList); vo.setCommonAccount(account); vo.setFrequency(count); vo.setCommonPersonName(commonPersonName); } return vo; } else { vo.setPersons(personVoList); vo.setCommonAccount(account); vo.setFrequency(count); vo.setCommonPersonName(commonPersonName); return vo; } } else { return vo; } }, executor); futureList.add(future); } voList.addAll(futureList.stream().map(CompletableFuture::join) .distinct().sorted(Comparator.comparing(vo -> vo.getPersons().size())) .collect(Collectors.toList()));
优化后的代码如下:
```java
List<CompletableFuture<CallIntersectionVo>> futureList = intersectionResult.entrySet().stream()
.map(entry -> CompletableFuture.supplyAsync(() -> {
String account = entry.getKey();
List<String> personNoList = entry.getValue().stream().distinct().collect(Collectors.toList());
CallIntersectionVo vo = new CallIntersectionVo();
if (personNoList.size() >= 2) {
List<PersonBasicVo> personVoList = personNoList.stream()
.map(personNo -> {
Map<String, Object> callMap = callMapList.stream()
.filter(map -> personNo.equals(map.get("personNo")))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found"));
List<CallRecord> callList = (List<CallRecord>) callMap.get("callList");
int count = (int) callList.stream().filter(x -> account.equals(x.getRelationshipAccount())).count();
return personList.stream()
.filter(person -> personNo.equals(person.getPersonNo()))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found"));
})
.collect(Collectors.toList());
String commonPersonName = getCommonPersonName(personList, account);
vo.setPersons(personVoList);
vo.setCommonAccount(account);
vo.setFrequency(personVoList.size());
vo.setCommonPersonName(commonPersonName);
return vo;
} else {
return vo;
}
}, executor))
.collect(Collectors.toList());
List<CallIntersectionVo> voList = futureList.stream()
.map(CompletableFuture::join)
.distinct()
.sorted(Comparator.comparingInt(vo -> vo.getPersons().size()))
.collect(Collectors.toList());
```
主要优化:
1. 使用流式编程,替换原来的for循环和entrySet遍历。
2. 使用`stream()`和`collect(Collectors.toList())`方法来收集结果,代替手动添加到列表中。
3. 使用`orElseThrow`方法来处理可能找不到元素的情况,避免空指针异常。
4. 将lambda表达式内联,使代码更简洁。
5. 使用`Comparator.comparingInt`方法来指定排序规则,避免编译器警告。
//1.获取最新两期日期 List<String> lastTwoDate = industryScoreInfoRepository.getLastTwoDate(); // List<String> lastTwoDate = new ArrayList<>(); // lastTwoDate.add("2023-02-28"); // lastTwoDate.add("2023-01-31"); log.info("当前观点较上期变化日期:"+new Gson().toJson(lastTwoDate)); //2.根据日期获取数据 List<IndustryScoreInfo> industryScoreInfoByDate = industryScoreInfoRepository.getIndustryLastScoreInfos(lastTwoDate); Map<String, List<IndustryScoreInfo>> groups = industryScoreInfoByDate.stream().collect(Collectors.groupingBy(IndustryScoreInfo::getTrackDate)); // 遍历groups中的每个List<IndustryScoreInfo> List<IndustryScoreResult> industryScoreResults = new ArrayList<>(); //获取当前最新日期综合得分 List<IndustryScoreInfo> industryScoreInfos = groups.get(lastTwoDate.get(0)); //获取上一日期综合得分 List<IndustryScoreInfo> last = groups.get(lastTwoDate.get(1)); for(IndustryScoreInfo industry: industryScoreInfos){ IndustryScoreResult result = new IndustryScoreResult(); result.setTrackDate(industry.getTrackDate()); result.setTrackName(industry.getTrackName()); //当前日期观点分数 result.setComprehensiveScore(industry.getComprehensiveScore()); //对上一日期遍历获取track_name相同时,的综合得分,赋值 如果上一日期不存在,则为空串("")返回 for(IndustryScoreInfo lastIndustryScoreInfo: last){ if(result.getTrackName().equals(lastIndustryScoreInfo.getTrackName())){ result.setLastComprehensiveScore(lastIndustryScoreInfo.getComprehensiveScore()); } } industryScoreResults.add(result); } 代码优化
可以将获取最新两期日期和根据日期获取数据的部分合并成一个流水线,这样可以避免重复的遍历和循环,代码如下:
```
List<IndustryScoreResult> industryScoreResults = industryScoreInfoRepository.getLastTwoDate().stream()
.map(date -> industryScoreInfoRepository.getIndustryLastScoreInfos(date))
.collect(Collectors.groupingBy(IndustryScoreInfo::getTrackDate))
.entrySet()
.stream()
.flatMap(entry -> {
List<IndustryScoreInfo> current = entry.getValue().stream().filter(info -> info.getTrackDate().equals(entry.getKey())).collect(Collectors.toList());
List<IndustryScoreInfo> last = entry.getValue().stream().filter(info -> !info.getTrackDate().equals(entry.getKey())).collect(Collectors.toList());
return current.stream().map(industry -> {
IndustryScoreResult result = new IndustryScoreResult();
result.setTrackDate(industry.getTrackDate());
result.setTrackName(industry.getTrackName());
result.setComprehensiveScore(industry.getComprehensiveScore());
Optional<IndustryScoreInfo> lastIndustryScoreInfo = last.stream().filter(info -> info.getTrackName().equals(industry.getTrackName())).findFirst();
result.setLastComprehensiveScore(lastIndustryScoreInfo.isPresent() ? lastIndustryScoreInfo.get().getComprehensiveScore() : "");
return result;
});
})
.collect(Collectors.toList());
```
这样代码更加简洁,并且利用了流式处理的优势,使得代码更加易于维护和扩展。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)