优化这段代码:List<CompletableFuture<ContactsIntersectionVo>> futureList = intersectionResult.entrySet().stream().map(entry -> CompletableFuture.supplyAsync(() -> { String account = entry.getKey(); List<String> personNoList = entry.getValue().stream().distinct().collect(Collectors.toList()); if (personNoList.size() >= 2) {// 取两个以上的交集 List<Map<String, Object>> remarkList = Lists.newArrayList(); List<PersonBasicVo> personVoList = Lists.newArrayList(); // 获取备注、涉案人 for (String personNo : personNoList) { Map<String, Object> contactsMap = contactsMapList.stream().filter(map -> personNo.equals(map.get("personNo"))).findAny().get(); List<ContactsBasic> contactsList = (List<ContactsBasic>) contactsMap.get("contactsList"); // 获取备注 for (ContactsBasic contacts : contactsList) { if (account.equals(contacts.getRelationshipAccount())) { PersonBasicVo personBasic = personList.stream().filter(person -> personNo.equals(person.getPersonNo())).findAny().get(); Map<String, Object> remarkMap = new HashMap<>(); remarkMap.put("name", personBasic.getName()); remarkMap.put("remark", contacts.getRelationshipName()); remarkList.add(remarkMap); break; } } // 获取涉案人 personVoList.add(personList.stream().filter(person -> personNo.equals(person.getPersonNo())).findAny().get()); } // 共同号码是否属于涉案人 String commonPersonName = getCommonPersonName(personList, account); ContactsIntersectionVo contactsVo = new ContactsIntersectionVo(); contactsVo.setRemarks(remarkList); contactsVo.setPersons(personVoList); contactsVo.setCommonAccount(account); contactsVo.setCommonPersonName(commonPersonName); return contactsVo; } else { return null; } }, executor)).collect(Collectors.toList()); contactisVoList.addAll(futureList.stream().map(CompletableFuture::join) .filter(Objects::nonNull) .collect(Collectors.toList()));
时间: 2024-04-12 15:31:05 浏览: 265
JDK8通过Stream 对List,Map操作和互转的实现
5星 · 资源好评率100%
优化后的代码如下:
```java
List<CompletableFuture<ContactsIntersectionVo>> futureList = intersectionResult.entrySet().stream()
.map(entry -> CompletableFuture.supplyAsync(() -> {
String account = entry.getKey();
List<String> personNoList = entry.getValue().stream().distinct().collect(Collectors.toList());
if (personNoList.size() >= 2) {// 取两个以上的交集
List<Map<String, Object>> remarkList = personNoList.stream()
.flatMap(personNo -> {
Map<String, Object> contactsMap = contactsMapList.stream()
.filter(map -> personNo.equals(map.get("personNo")))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found"));
List<ContactsBasic> contactsList = (List<ContactsBasic>) contactsMap.get("contactsList");
PersonBasicVo personBasic = personList.stream()
.filter(person -> personNo.equals(person.getPersonNo()))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found"));
return contactsList.stream()
.filter(contacts -> account.equals(contacts.getRelationshipAccount()))
.flatMap(contacts -> Stream.of(Map.of("name", personBasic.getName(), "remark", contacts.getRelationshipName())));
})
.collect(Collectors.toList());
List<PersonBasicVo> personVoList = personNoList.stream()
.map(personNo -> personList.stream()
.filter(person -> personNo.equals(person.getPersonNo()))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found")))
.collect(Collectors.toList());
String commonPersonName = getCommonPersonName(personList, account);
ContactsIntersectionVo contactsVo = new ContactsIntersectionVo();
contactsVo.setRemarks(remarkList);
contactsVo.setPersons(personVoList);
contactsVo.setCommonAccount(account);
contactsVo.setCommonPersonName(commonPersonName);
return contactsVo;
} else {
return null;
}
}, executor))
.collect(Collectors.toList());
contactisVoList.addAll(futureList.stream()
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
```
主要优化:
1. 使用流式编程,替换原来的for循环和entrySet遍历。
2. 使用`stream()`和`collect(Collectors.toList())`方法来收集结果,代替手动添加到列表中。
3. 使用`flatMap`方法来处理嵌套的列表。
4. 使用`orElseThrow`方法来处理可能找不到元素的情况,避免空指针异常。
5. 将lambda表达式内联,使代码更简洁。
阅读全文