分析这段代码 List<Map> params = paperQuestion.getQuestionIdsList().stream().map(question -> { Map mp = new HashMap(); mp.put("paperId", paperQuestion.getPaperId()); mp.put("questionId", question.getId()); return mp; }).collect(Collectors.toList());
时间: 2023-06-24 21:07:40 浏览: 66
这段代码使用了Java 8中的Stream API,对List<PaperQuestion>对象中的questionIdsList属性进行了处理,将其转换为List<Map>类型的对象params。
具体的处理流程是:首先使用stream()方法将List<PaperQuestion>对象中的questionIdsList属性转换为Stream类型的对象,然后使用map()方法对其中的每一个元素进行映射操作。在这里,使用了Lambda表达式,将每一个Question对象转换为一个Map对象,其中Map的key为"paperId",value为PaperQuestion对象的paperId属性的值;Map的key为"questionId",value为Question对象的id属性的值。最后,使用collect()方法将所有Map对象收集起来,转换为List类型的对象。
总的来说,这段代码的作用是将List<PaperQuestion>对象中的questionIdsList属性中的每一个Question对象转换为一个包含paperId和questionId属性的Map对象,并将所有Map对象组成一个List返回。
相关问题
讲解一下这段代码List<String> list = map.getOrDefault(key, new ArrayList<String>());
这段代码的作用是从一个Map对象中获取与指定键关联的值,如果该键不存在,则返回一个新的ArrayList对象。具体解释如下:
1. map.getOrDefault(key, defaultValue)方法是Map接口中的一个方法,它的作用是获取与指定键关联的值。如果该键存在,则返回与该键关联的值;如果该键不存在,则返回defaultValue。
2. 在这段代码中,key是指定的键,map是要获取值的Map对象,而new ArrayList<String>()则是默认的值。如果指定的键不存在,则返回一个新的ArrayList对象。
3. 最后,将返回的值赋给一个List<String>类型的变量list,这个变量就是我们想要的与指定键关联的值。
总之,这段代码的作用是获取Map对象中指定键的值,并且如果该键不存在,则返回一个空的ArrayList对象。
List<DataPermissionSchemaEo> dataPermissionSchemaEoList = comDataComponent.getDataPermissionSchemaList(); List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList(); List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList(); //设置行权限 根据用户id和用户组织去获取 List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream() .filter(schemaRowRuleEo -> { List<String> userList = JSONObject.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); List<String> orgList = JSONObject.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); return userList.contains(userUid) || orgList.contains(orgCode) || userList.contains(WILDCARD) || orgList.contains(WILDCARD); }) .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 -> JSONObject.parseArray(userListStr, String.class)) .orElse(Collections.emptyList()); List<String> orgList = Optional.ofNullable(rule.getColumnPermissionOrgList()) .map(orgListStr -> JSONObject.parseArray(orgListStr, String.class)) .orElse(Collections.emptyList()); return userList.contains(userUid) || orgList.contains(orgCode) || userList.contains(WILDCARD) || orgList.contains(WILDCARD); }) .map(rule -> { SchemaColumnRule columnRule = new SchemaColumnRule(); BeanUtils.copyProperties(rule, columnRule); return columnRule; }) .collect(Collectors.toList()); List<DataPermissionSchema> dataPermissionSchemaList = dataPermissionSchemaEoList.stream().map(dataPermissionSchemaEo -> { List<SchemaRowRule> schemaRowRules = userRowRuleList.stream() .filter(schemaRule -> dataPermissionSchemaEo.getDatabaseCode().equals(schemaRule.getDatabaseCode()) && dataPermissionSchemaEo.getSchemaCode().equals(schemaRule.getSchemaCode())) .collect(Collectors.toList()); List<SchemaColumnRule> schemaColumnRules = userColumnRuleList.stream() .filter(schemaRule -> dataPermissionSchemaEo.getDatabaseCode().equals(schemaRule.getDatabaseCode()) && dataPermissionSchemaEo.getSchemaCode().equals(schemaRule.getSchemaCode())) .collect(Collectors.toList()); if(!schemaRowRules.isEmpty() || !schemaColumnRules.isEmpty()) { DataPermissionSchema dataPermissionSchema = new DataPermissionSchema(); dataPermissionSchema.setDatabaseCode(dataPermissionSchemaEo.getDatabaseCode()); dataPermissionSchema.setSchemaCode(dataPermissionSchemaEo.getSchemaCode()); dataPermissionSchema.setSchemaRowRuleList(schemaRowRules); dataPermissionSchema.setSchemaColumnRuleList(schemaColumnRules); return dataPermissionSchema; } return null; }).filter(Objects::nonNull).collect(Collectors.toList());把这段代码改造成每个用户拥有的行权限和列权限
可以将该代码封装成一个方法,接收用户ID和组织代码作为参数,返回该用户拥有的行权限和列权限,代码如下:
```
public List<DataPermissionSchema> getUserDataPermissionSchema(String userUid, String orgCode) {
List<DataPermissionSchemaEo> dataPermissionSchemaEoList = comDataComponent.getDataPermissionSchemaList();
List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList();
List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList();
// 获取用户行权限
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> {
List<String> userList = JSONObject.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class);
List<String> orgList = JSONObject.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class);
return userList.contains(userUid) || orgList.contains(orgCode) || userList.contains(WILDCARD) || orgList.contains(WILDCARD);
})
.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 -> JSONObject.parseArray(userListStr, String.class))
.orElse(Collections.emptyList());
List<String> orgList = Optional.ofNullable(rule.getColumnPermissionOrgList())
.map(orgListStr -> JSONObject.parseArray(orgListStr, String.class))
.orElse(Collections.emptyList());
return userList.contains(userUid) || orgList.contains(orgCode) || userList.contains(WILDCARD) || orgList.contains(WILDCARD);
})
.map(rule -> {
SchemaColumnRule columnRule = new SchemaColumnRule();
BeanUtils.copyProperties(rule, columnRule);
return columnRule;
})
.collect(Collectors.toList());
// 获取用户权限所在的数据权限模板
List<DataPermissionSchema> dataPermissionSchemaList = dataPermissionSchemaEoList.stream().map(dataPermissionSchemaEo -> {
List<SchemaRowRule> schemaRowRules = userRowRuleList.stream()
.filter(schemaRule -> dataPermissionSchemaEo.getDatabaseCode().equals(schemaRule.getDatabaseCode())
&& dataPermissionSchemaEo.getSchemaCode().equals(schemaRule.getSchemaCode()))
.collect(Collectors.toList());
List<SchemaColumnRule> schemaColumnRules = userColumnRuleList.stream()
.filter(schemaRule -> dataPermissionSchemaEo.getDatabaseCode().equals(schemaRule.getDatabaseCode())
&& dataPermissionSchemaEo.getSchemaCode().equals(schemaRule.getSchemaCode()))
.collect(Collectors.toList());
if(!schemaRowRules.isEmpty() || !schemaColumnRules.isEmpty()) {
DataPermissionSchema dataPermissionSchema = new DataPermissionSchema();
dataPermissionSchema.setDatabaseCode(dataPermissionSchemaEo.getDatabaseCode());
dataPermissionSchema.setSchemaCode(dataPermissionSchemaEo.getSchemaCode());
dataPermissionSchema.setSchemaRowRuleList(schemaRowRules);
dataPermissionSchema.setSchemaColumnRuleList(schemaColumnRules);
return dataPermissionSchema;
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
return dataPermissionSchemaList;
}
```
阅读全文