优化这段代码: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()));
时间: 2024-04-12 10:30:27 浏览: 144
优化后的代码如下:
```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`方法来指定排序规则,避免编译器警告。
阅读全文