优化代码 private void addReferencedKpi(List<SimulationTableDto> list, List<QueryCodeVo> 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(); IntStream.range(0, codeList.size()).forEach(i -> { CodeVo codeVo = codeList.get(i).getCode(); String title = codeVo.getTitle(); Map<String, List<String>> referencedPerMap = referencedList.get(i); if (MapUtils.isEmpty(referencedPerMap)) { return; } referencedPerMap.forEach((table, columns) -> { String reversedTable = table; // case表别名去掉case后缀 if (reversedTable.endsWith(PqlConst.CASES_TABLE_NAME_SUFFIX)) { reversedTable = reversedTable.substring(0, reversedTable.length() - PqlConst.CASES_TABLE_NAME_SUFFIX.length()); } tableColumnWithCodeListMap.computeIfAbsent(reversedTable, k -> Maps.newHashMap()); String finalReversedTable = reversedTable; columns.forEach(column -> tableColumnWithCodeListMap.get(finalReversedTable).computeIfAbsent(column, k -> Lists.newArrayList()).add(title)); }); }); 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; }); }); }
时间: 2024-01-04 22:03:28 浏览: 106
这段代码已经很简洁了,但还有一些可以改进的地方。首先,可以使用 Stream 的方式来实现对 QueryCodeVo 列表的遍历,这样可以更加简洁地实现相同的逻辑。其次,可以将对 tableColumnWithCodeListMap 的操作提取为一个方法,这样可以使代码更加清晰。以下是优化后的代码:
```
private void addReferencedKpi(List<SimulationTableDto> list, List<QueryCodeVo> codeList, List<Map<String, List<String>>> referencedList) {
if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) {
return;
}
Map<String, Map<String, List<String>>> tableColumnWithCodeListMap = new HashMap<>();
codeList.stream()
.map(QueryCodeVo::getCode)
.forEach(codeVo -> {
String title = codeVo.getTitle();
Map<String, List<String>> referencedPerMap = referencedList.get(codeList.indexOf(new QueryCodeVo(codeVo)));
if (MapUtils.isEmpty(referencedPerMap)) {
return;
}
referencedPerMap.forEach((table, columns) -> {
String reversedTable = table.endsWith(PqlConst.CASES_TABLE_NAME_SUFFIX) ? table.substring(0, table.length() - PqlConst.CASES_TABLE_NAME_SUFFIX.length()) : table;
addReferencedKpiToMap(reversedTable, columns, title, tableColumnWithCodeListMap);
});
});
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;
});
});
}
private void addReferencedKpiToMap(String reversedTable, List<String> columns, String title, Map<String, Map<String, List<String>>> tableColumnWithCodeListMap) {
tableColumnWithCodeListMap.computeIfAbsent(reversedTable, k -> new HashMap<>());
columns.forEach(column -> tableColumnWithCodeListMap.get(reversedTable)
.computeIfAbsent(column, k -> new ArrayList<>()).add(title));
}
```
优化后的代码使用了 Stream 对 QueryCodeVo 列表进行遍历,使用了一个额外的方法 addReferencedKpiToMap 来添加 KPI 到 tableColumnWithCodeListMap。这样代码更加清晰和简洁。
阅读全文