List<SysDictDataDTO> collect = dictDataDTOS.stream().filter(x -> key.equals(x.getDictLabel())).collect(Collectors.toList())
时间: 2024-05-21 12:12:05 浏览: 111
好的,您的第一个问题是关于 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`方法来指定排序规则,避免编译器警告。
优化这段代码: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表达式内联,使代码更简洁。
阅读全文