List<Map<String, Object>> columns = Arrays.stream(handover.getDeclaredFields()).map(field -> { Map<String, Object> map = new HashMap<>(3); String name = field.getAnnotation(ApiModelProperty.class).value(); if (tableindex.contains(name)) { map.put("title", name); map.put("dataIndex", field.getName()); map.put("key", field.getName()); } return map; }).filter(o-> o.size()>0 ).collect(Collectors.toList());解释下这段代码
时间: 2023-04-01 15:03:47 浏览: 148
这段代码是使用 Java 8 的 Stream API 对一个类的属性进行处理,将其转换为一个包含 Map 的 List。其中,Map 中包含三个键值对,分别为 title、dataIndex 和 key,对应的值分别为属性的注解值、属性名和属性名。在转换过程中,还进行了过滤,只保留包含 tableindex 中的属性。最终,返回一个 List<Map<String, Object>> 类型的结果。
相关问题
优化代码 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; }); }); }
这段代码已经很简洁了,但还有一些可以改进的地方。首先,可以使用 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。这样代码更加清晰和简洁。
优化 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;
});
});
}
```
阅读全文