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 10:03:47 浏览: 55
这段代码是使用 Java 8 的 Stream API 对一个类的属性进行处理,将其转换为一个包含 Map 的 List。其中,Map 中包含三个键值对,分别为 title、dataIndex 和 key,对应的值分别为属性的注解值、属性名和属性名。在转换过程中,还进行了过滤,只保留包含 tableindex 中的属性。最终,返回一个 List<Map<String, Object>> 类型的结果。
相关问题
将 List<Map<String, Object>> 对象转化为 ResultSet 对象。
将 List<Map<String, Object>> 对象转化为 ResultSet 对象可以通过使用 ResultSetMetaData 接口来实现,以下是示例代码:
```java
public ResultSet convertListMapToResultSet(List<Map<String, Object>> list) throws SQLException {
// 创建一个空的 ResultSetMetaData 对象
ResultSetMetaData metaData = null;
// 创建一个空的 ResultSet 对象
ResultSet resultSet = null;
if (list != null && !list.isEmpty()) {
// 获取 Map 中的所有键值对
Set<String> columns = list.get(0).keySet();
// 创建一个新的 ResultSetMetaData 对象
metaData = new ResultSetMetaData() {
@Override
public int getColumnCount() throws SQLException {
return columns.size();
}
// 其他方法省略,可根据实际需要实现
};
// 使用 ResultSetMetaData 和 List<Map<String, Object>> 创建一个 ResultSet 对象
resultSet = new ResultSet() {
// 当前行索引
private int rowIndex = -1;
// 当前行的 Map 对象
private Map<String, Object> currentRow = null;
@Override
public boolean next() throws SQLException {
if (rowIndex + 1 < list.size()) {
rowIndex++;
currentRow = list.get(rowIndex);
return true;
}
return false;
}
@Override
public ResultSetMetaData getMetaData() throws SQLException {
return metaData;
}
@Override
public Object getObject(int columnIndex) throws SQLException {
String columnName = metaData.getColumnName(columnIndex);
return currentRow.get(columnName);
}
// 其他方法省略,可根据实际需要实现
};
}
return resultSet;
}
```
需要注意的是,上述代码中创建的 ResultSet 对象只是一个伪造的 ResultSet,不能进行修改操作。如果需要对 ResultSet 进行修改操作,可以考虑使用第三方库,如 Apache 的 Commons DbUtils。
优化代码 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。这样代码更加清晰和简洁。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)