List<BlogLink> links = blogLinkMapper.findLinkList(null); if (!CollectionUtils.isEmpty(links)) { Map<Byte, List<BlogLink>> linksMap = links.stream().collect(Collectors.groupingBy(BlogLink::getLinkType)); return linksMap; }
时间: 2023-07-15 16:10:02 浏览: 148
这段代码使用了MyBatis框架进行数据库查询,查询结果返回一个BlogLink对象的List。然后通过Java 8的Stream API对这个List进行操作,使用groupingBy方法将其按照BlogLink对象的linkType属性进行分组,最终得到一个Map对象,其中key为linkType属性值,value为相应的BlogLink对象List。如果links为空,则返回一个空Map对象。
相关问题
优化 private void addReferencedKpi(List<SimulationTableDto> list, List<String> codeList, List<Map<String, List<String>>> referencedList) { if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) { return; } // 转换结构 key1-table key2-column value-codeList Map<String, Map<String, List<String>>> tableColumnWithCodeListMap = Maps.newHashMap(); int size = referencedList.size(); for (int i = 0; i < size; i++) { String code = codeList.get(i); Map<String, List<String>> referencedPerMap = referencedList.get(i); if (MapUtils.isEmpty(referencedPerMap)) { continue; } referencedPerMap.forEach((table, columns) -> { tableColumnWithCodeListMap.putIfAbsent(table, Maps.newHashMap()); for (String column : columns) { tableColumnWithCodeListMap.get(table).putIfAbsent(column, Lists.newArrayList()); tableColumnWithCodeListMap.get(table).get(column).add(code); } }); } for (SimulationTableDto simulationTableDto : list) { String simTableName = simulationTableDto.getTableName(); String simTitle = simulationTableDto.getColumn().getTitle(); if (!tableColumnWithCodeListMap.containsKey(simTableName) || !tableColumnWithCodeListMap.get(simTableName).containsKey(simTitle)) { continue; } List<String> referencedCodeList = tableColumnWithCodeListMap.get(simTableName).get(simTitle); simulationTableDto.getColumn().setReferencedKpis(referencedCodeList); } }
可以尝试优化一下代码结构,减少嵌套层数,使得代码更加易读和易维护。例如可以将转换结构的代码抽取成一个单独的方法,用于处理每个 referencedPerMap 中的数据,并返回转换后的表格和列名以及对应的 codeList 的 Map 对象。同时,可以考虑使用 Java 8 的 Stream API,更加简洁地实现循环操作。修改后的代码如下:
```
private void addReferencedKpi(List<SimulationTableDto> list, List<String> codeList, List<Map<String, List<String>>> referencedList) {
if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) {
return;
}
// 转换结构 key1-table key2-column value-codeList
Map<String, Map<String, List<String>>> tableColumnWithCodeListMap = new HashMap<>();
referencedList.forEach(referencedPerMap -> {
if (MapUtils.isEmpty(referencedPerMap)) {
return;
}
referencedPerMap.forEach((table, columns) -> {
tableColumnWithCodeListMap.computeIfAbsent(table, k -> new HashMap<>());
columns.forEach(column -> tableColumnWithCodeListMap.get(table).computeIfAbsent(column, k -> new ArrayList<>()).addAll(codeList));
});
});
list.forEach(simulationTableDto -> {
String simTableName = simulationTableDto.getTableName();
String simTitle = simulationTableDto.getColumn().getTitle();
tableColumnWithCodeListMap.computeIfPresent(simTableName, (table, columnMap) -> {
if (columnMap.containsKey(simTitle)) {
simulationTableDto.getColumn().setReferencedKpis(columnMap.get(simTitle));
}
return columnMap;
});
});
}
```
//获取用户权限 List<DataPermissionSchemaEo> dataPermissionsRedisByUserUid = DataPermissionUtils.getDataPermissionsRedisByUserUid(UserAgent.get().getUserId()); if (CollectionUtils.isEmpty(dataPermissionsRedisByUserUid)) { return joinPoint.proceed(); } Map<String, DataPermissionSchemaEo> dataPermissionSchemaEoMap = dataPermissionsRedisByUserUid.stream().collect(Collectors.toMap(key -> key.getDatabaseCode() + key.getSchemaCode(), value -> value)); //获取用户对于这个表的行权限 DataPermissionSchemaEo dataPermissionSchemaEo = dataPermissionSchemaEoMap.get(databaseName + tableName); if(dataPermissionSchemaEo == null){ return joinPoint.proceed(); } List<SchemaRowRuleEo> schemaRowRuleList = dataPermissionSchemaEo.getSchemaRowRuleList(); if(CollectionUtils.isEmpty(schemaRowRuleList)){ return joinPoint.proceed(); }帮我优化这段代码
可以将代码优化为:
```java
List<DataPermissionSchemaEo> dataPermissionsRedisByUserUid = DataPermissionUtils.getDataPermissionsRedisByUserUid(UserAgent.get().getUserId());
if (CollectionUtils.isEmpty(dataPermissionsRedisByUserUid)) {
return joinPoint.proceed();
}
DataPermissionSchemaEo dataPermissionSchemaEo = dataPermissionsRedisByUserUid.stream()
.filter(d -> (d.getDatabaseCode() + d.getSchemaCode()).equals(databaseName + tableName))
.findFirst()
.orElse(null);
if (dataPermissionSchemaEo == null) {
return joinPoint.proceed();
}
List<SchemaRowRuleEo> schemaRowRuleList = dataPermissionSchemaEo.getSchemaRowRuleList();
if (CollectionUtils.isEmpty(schemaRowRuleList)) {
return joinPoint.proceed();
}
```
这里采用了:
1. 使用 Stream API 的 filter() 和 findFirst() 方法来优雅地查找用户对于这个表的行权限。
2. 使用 orElse(null) 方法来取代 if 判断,使得代码更加简洁。
3. 将变量声明和初始化放在一起,提高代码的可读性。
阅读全文