List<RsysMenu> rootMenus = list.stream().filter(item -> "M".equals(item.getParentId())).sorted().collect(Collectors.toList());
时间: 2024-04-21 08:25:11 浏览: 14
这段代码使用了Java 8的流式操作,实现了以下功能:
1. 根据条件筛选出父菜单ID为"M"的元素。
2. 对筛选出的元素进行排序。
3. 将排序后的元素收集到一个新的List中。
具体的代码解释如下:
- `list` 是一个包含了 `RsysMenu` 对象的列表。
- `stream()` 方法将列表转换为一个流,以便进行后续的操作。
- `filter(item -> "M".equals(item.getParentId()))` 使用 `filter` 方法对流中的元素进行筛选,只保留父菜单ID为"M"的元素。
- `sorted()` 方法对筛选出的元素进行排序,默认使用元素的自然顺序。
- `collect(Collectors.toList())` 将排序后的元素收集到一个新的List中。
最终,`rootMenus` 变量将包含符合条件并排序后的父菜单对象。
相关问题
优化这段代码: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`方法来指定排序规则,避免编译器警告。
menuList = allMenuList.stream() .filter(menu ->("-1").equals(menu.getParentId())) .peek(menu -> menu.setChildren(listChildMenu(isSelectButton, allMenuList, menu.getMenuId()))) .sorted(Comparator.comparingInt(MenuTreeVO::getOrderNum)) .collect(Collectors.toList()); 请解读一下这段代码
这段代码使用Java 8中的Stream API对一个名为allMenuList的菜单列表进行处理,并返回一个名为menuList的新列表。该列表中只包含顶级菜单(即parentId等于-1),并按照orderNum属性进行排序。
在代码中,使用filter方法过滤出parentId等于-1的菜单,并使用peek方法对这些菜单进行操作。peek方法的作用是对流中的每个元素执行操作,并返回一个与原始流相同的新流。在这里,peek方法的操作是设置每个菜单的子菜单(即子菜单的parentId等于当前菜单的menuId),并使用listChildMenu方法获取子菜单列表。listChildMenu方法是一个自定义方法,用于获取子菜单列表。
最后,使用sorted方法根据orderNum属性对菜单进行排序,然后使用collect方法将结果收集到一个新的列表中,并返回该列表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)