beanutils.copyproperties复制list<map>busilist
时间: 2024-05-08 12:21:11 浏览: 169
`BeanUtils.copyProperties` 方法只能用于单个 Java 对象之间的属性复制,无法用于列表/集合中的元素属性复制。如果要复制 List<Map> 中的元素属性,可以使用循环遍历列表,对每个 Map 元素进行属性复制。示例代码如下:
```java
for (Map<String, Object> busi : busilist) {
MyBean myBean = new MyBean();
BeanUtils.copyProperties(myBean, busi);
// TODO: 对 myBean 进行操作
}
```
其中,`MyBean` 为目标 Java Bean 类型,`busilist` 是待复制的 List<Map> 集合。在循环中,对每个 Map 元素进行属性复制,并将复制后的 Java Bean 对象 `myBean` 用于后续的操作。
相关问题
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表达式,代码如下:
```
private <T> List<T> copyProperties(List<T> sourceList, Supplier<T> targetSupplier) {
return sourceList.stream()
.map(source -> {
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
return target;
})
.collect(Collectors.toList());
}
...
List<String> userList = parseJsonArray(schemaRowRuleEo.getRowPermissionUserList());
List<String> orgList = parseJsonArray(schemaRowRuleEo.getRowPermissionOrgList());
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> userList.contains(userUid) || orgList.contains(orgCode))
.collect(Collectors.toList());
List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream()
.filter(rule -> {
List<String> userList = parseJsonArray(rule.getColumnPermissionUserList());
List<String> orgList = parseJsonArray(rule.getColumnPermissionOrgList());
return userList.contains(userUid) || orgList.contains(orgCode);
})
.collect(Collectors.toList());
List<SchemaRowRule> copiedRowRuleList = copyProperties(userRowRuleList, SchemaRowRule::new);
List<SchemaColumnRule> copiedColumnRuleList = copyProperties(userColumnRuleList, SchemaColumnRule::new);
...
private List<String> parseJsonArray(String jsonArray) {
if (StringUtils.isNotEmpty(jsonArray)) {
return JSONArray.parseArray(jsonArray, String.class);
}
return Collections.emptyList();
}
```
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表达式进一步简化代码:
```
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> {
List<String> userList = JSON.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class);
List<String> orgList = JSON.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(userListStr -> JSON.parseArray(userListStr, String.class))
.orElse(Collections.emptyList());
List<String> orgList = Optional.ofNullable(rule.getColumnPermissionOrgList())
.map(orgListStr -> JSON.parseArray(orgListStr, String.class))
.orElse(Collections.emptyList());
return userList.contains(userUid) || orgList.contains(orgCode);
})
.map(rule -> {
SchemaColumnRule columnRule = new SchemaColumnRule();
BeanUtils.copyProperties(rule, columnRule);
return columnRule;
})
.collect(Collectors.toList());
```
这里使用了Optional类来避免空指针异常,对于可能为空的列表,可以使用`Optional.ofNullable()`方法将其转换为Optional对象,然后使用`map()`方法进行转换或默认值设置。同时,使用lambda表达式可以更加简洁地实现函数式编程。
阅读全文