maps.forEach
时间: 2023-11-09 08:00:11 浏览: 148
`maps.forEach`是JavaScript中用于遍历Map对象的方法。它接受一个回调函数作为参数,该回调函数会依次接收Map中的每个元素作为参数,并执行相应的操作。例如,以下代码会输出Map中每个元素的键和值:
```
const myMap = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three']
]);
myMap.forEach((value, key) => {
console.log(key + ' = ' + value);
});
```
输出结果如下:
```
1 = one
2 = two
3 = three
```
相关问题
this.maps.forEach((item, key) => { const { node, child, resolve } = this.maps.get(key); this.treeLoad(node, child, resolve); });这段代码什么意思
这段代码是使用ES6语法中的箭头函数遍历一个Map对象,并且在遍历过程中解构每个Map对象的key对应的value,然后调用treeLoad方法。具体来说,这段代码的功能是遍历Map对象this.maps,并且对于每一个Map对象的key,从Map对象中获取该key对应的value,然后解构出value对象中的node、child和resolve属性,并将这些属性作为参数传递给treeLoad方法。这段代码的目的是执行treeLoad方法来加载一棵树的节点和子节点。
优化代码 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。这样代码更加清晰和简洁。
阅读全文