controlFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());什么意思
时间: 2023-05-24 08:03:45 浏览: 132
A:这是Java中使用CompletableFuture的语法。它的含义是:使用流(stream),调用controlFutures集合中每一个CompletableFuture对象的join方法获取结果,并将这些结果收集到一个新的列表中。CompletableFuture是一种Java中的异步编程机制,它可以在后台执行多个任务,并在所有任务完成后返回结果。此处代码的作用是等待所有的CompletableFuture任务完成,并将它们的结果收集到一个列表中。
相关问题
优化这段代码:List<CompletableFuture<Map<String, Object>>> futureList = new ArrayList<>(); for (PersonBasicVo person : personList) { CompletableFuture<Map<String, Object>> future = CompletableFuture.supplyAsync(() -> { String personNo = person.getPersonNo(); List<ContactsBasic> contactsList = contactsService.list(Wrappers.lambdaQuery(ContactsBasic.class) .eq(ContactsBasic::getPersonNo, personNo) .ne(ContactsBasic::getRelationshipAccount, "") .isNotNull(ContactsBasic::getPersonNo) .like(StrUtil.isNotBlank(phone), ContactsBasic::getRelationshipAccount, phone)); if (CollUtil.isNotEmpty(contactsList)) { Map<String, Object> contactsMap = new HashMap<>(); contactsMap.put("personNo", personNo); contactsMap.put("contactsList", contactsList); return contactsMap; } else { return null; } }, executor); futureList.add(future); } List<Map<String, Object>> contactsMapList = futureList.stream() .map(CompletableFuture::join) .filter(Objects::nonNull) .collect(Collectors.toList());
优化后的代码如下:
```java
List<CompletableFuture<Map<String, Object>>> futureList = personList.stream()
.map(person -> CompletableFuture.supplyAsync(() -> {
String personNo = person.getPersonNo();
List<ContactsBasic> contactsList = contactsService.list(Wrappers.lambdaQuery(ContactsBasic.class)
.eq(ContactsBasic::getPersonNo, personNo)
.ne(ContactsBasic::getRelationshipAccount, "")
.isNotNull(ContactsBasic::getPersonNo)
.like(StrUtil.isNotBlank(phone), ContactsBasic::getRelationshipAccount, phone));
if (CollUtil.isNotEmpty(contactsList)) {
Map<String, Object> contactsMap = new HashMap<>();
contactsMap.put("personNo", personNo);
contactsMap.put("contactsList", contactsList);
return contactsMap;
} else {
return null;
}
}, executor))
.collect(Collectors.toList());
List<Map<String, Object>> contactsMapList = futureList.stream()
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList());
```
主要优化:
1. 使用流式编程,替换原来的for循环。
2. 使用`stream()`和`collect(Collectors.toList())`方法来收集结果,代替手动添加到列表中。
3. 将lambda表达式内联,使代码更简洁。
优化这段代码: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()));
优化后的代码如下:
```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表达式内联,使代码更简洁。
阅读全文