List<String> fundCodes = pcfFileEntities.stream().filter(pcfFileEntityPredicate).map(pcfFileEntity -> pcfFileEntity.getSecurityId()).collect(Collectors.toList());
时间: 2024-02-26 14:53:44 浏览: 24
这段代码使用 Java 8 中的 Stream API,对一个 List<pcfFileEntity> 进行过滤、映射和收集操作,最终得到一个 List<String>。
具体来说,pcfFileEntities 是一个 List,其中包含多个 pcfFileEntity 对象。代码中的 filter() 方法接受一个 Predicate<pcfFileEntity> 参数,用于筛选出符合条件的 pcfFileEntity 对象。map() 方法将筛选出来的 pcfFileEntity 对象映射为其 securityId 属性的值。最后,collect() 方法将映射后的所有 securityId 值收集到一个 List<String> 中,并返回。
相关问题
List<String> collect = lists.stream().map(t -> t.getBusinessid()).collect(Collectors.toList());
这是一个 Java 8 中使用 Stream API 对一个 List 进行操作的示例代码。它的作用是从一个 List 中获取每个元素的 businessid 属性,并将它们收集到一个新的 List 中。
具体来说,假设我们有一个 List<SomeObject> 类型的 lists 变量,其中 SomeObject 类型的对象具有一个名为 businessid 的属性。我们可以通过 Stream API 的 map() 方法将每个元素转换为它的 businessid 属性,最后通过 collect() 方法将所有转换后的值收集到一个新的 List<String> 类型的 collect 变量中。
需要注意的是,这段代码中使用了 Java 8 中的 Lambda 表达式,它的作用是将一个 SomeObject 类型的对象 t 转换为它的 businessid 属性。在 Stream API 中,Lambda 表达式可以被用来简洁地表达一些操作,例如 map()、filter()、reduce() 等等。
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream() .filter(schemaRowRuleEo -> { List<String> userList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); List<String> orgList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); return userList.contains(userUid) || orgList.contains(orgCode); }) .map(schemaRowRuleEo -> { SchemaRowRule schemaRowRule = SchemaRowRule.builder().build(); BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule); return schemaRowRule; }) .collect(Collectors.toList()); List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream() .filter(rule -> { if (StringUtils.isNotEmpty(rule.getColumnPermissionUserList())) { List<String> userList = JSONArray.parseArray(rule.getColumnPermissionUserList(), String.class); return userList.contains(userUid); } else if (StringUtils.isNotEmpty(rule.getColumnPermissionOrgList())) { List<String> orgList = JSONArray.parseArray(rule.getColumnPermissionOrgList(), String.class); return orgList.contains(orgCode); } return false; }) .map(rule -> { SchemaColumnRule columnRule = new SchemaColumnRule(); BeanUtils.copyProperties(rule, columnRule); return columnRule; }) .collect(Collectors.toList());优化代码
可以考虑使用方法引用来简化Lambda表达式,同时避免多次解析JSON字符串,代码如下:
```
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> {
List<String> userList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class);
List<String> orgList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class);
return userList.contains(userUid) || orgList.contains(orgCode);
})
.map(schemaRowRuleEo -> {
SchemaRowRule schemaRowRule = new SchemaRowRule();
BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule);
return schemaRowRule;
})
.collect(Collectors.toList());
List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream()
.filter(rule -> {
List<String> userList = Optional.ofNullable(rule.getColumnPermissionUserList())
.map(jsonArray -> JSONArray.parseArray(jsonArray, String.class))
.orElse(Collections.emptyList());
List<String> orgList = Optional.ofNullable(rule.getColumnPermissionOrgList())
.map(jsonArray -> JSONArray.parseArray(jsonArray, String.class))
.orElse(Collections.emptyList());
return userList.contains(userUid) || orgList.contains(orgCode);
})
.map(BeanUtils::instantiate)
.collect(Collectors.toList());
```
其中,使用了`Optional`来避免`null`值的判断和处理,使用`BeanUtils::instantiate`方法引用来创建对象并复制属性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)