帮我优化一下 Map<String, MallHomePage> budgetAchievementReport = inDataAdaptor.getBudgetAchievementReport(); if (!CollectionUtils.isEmpty(budgetAchievementReport)){ if (budgetAchievementReport.containsKey(mallCoed)) { MallHomePage mallHomePage = budgetAchievementReport.get(mallCoed); berthWarningDto.setRentalRate(new BigDecimal(1).subtract(mallHomePage.getTotal_rented_rate().setScale(2, BigDecimal.ROUND_HALF_UP))); } }
时间: 2023-05-19 19:02:18 浏览: 95
可以尝试使用 Optional 类型和函数式编程来简化代码,如下所示:
Optional<MallHomePage> mallHomePageOptional = Optional.ofNullable(inDataAdaptor.getBudgetAchievementReport().get(mallCode));
mallHomePageOptional.ifPresent(mallHomePage -> berthWarningDto.setRentalRate(BigDecimal.ONE.subtract(mallHomePage.getTotal_rented_rate().setScale(2, BigDecimal.ROUND_HALF_UP))));
相关问题
优化 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;
});
});
}
```
优化 private void addReferencedKpi(List<AnalysisTableDto> 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); } }); } // 装饰引用kpi for (AnalysisTableDto simulationTableDto : list) { String simTableName = simulationTableDto.getTableName(); for (SimulationColumnDto column : simulationTableDto.getColumns()) { String simTitle = column.getTitle(); if (!tableColumnWithCodeListMap.containsKey(simTableName) || !tableColumnWithCodeListMap.get(simTableName).containsKey(simTitle)) { continue; } List<String> referencedCodeList = tableColumnWithCodeListMap.get(simTableName).get(simTitle); column.setReferencedKpis(referencedCodeList); } } }
可以考虑以下几点进行优化:
1. 将代码分解为更小的方法,提高可读性和可维护性。
2. 使用Java 8+的Stream API代替循环和条件语句,使代码更简洁。
3. 可以使用Google Guava库提供的Multimap类来代替手动创建的tableColumnWithCodeListMap,从而简化代码。
4. 使用针对null的断言操作符(Java 11+)来简化空值检查。
下面是参考示例代码:
```java
private void addReferencedKpi(List<AnalysisTableDto> list, List<String> codeList, List<Map<String, List<String>>> referencedList) {
if (list == null || codeList == null || referencedList == null) {
return;
}
// 转换结构 key1-table key2-column value-codeList
Multimap<String, String> tableColumnWithCodeListMap = Multimaps.newListMultimap(
Maps.newHashMap(), () -> Lists.newArrayList());
IntStream.range(0, codeList.size())
.forEach(i -> {
String code = codeList.get(i);
Map<String, List<String>> referencedPerMap = referencedList.get(i);
if (referencedPerMap != null) {
referencedPerMap.forEach((table, columns) ->
columns.forEach(column ->
tableColumnWithCodeListMap.put(table + "_" + column, code)));
}
});
// 装饰引用kpi
list.forEach(tableDto ->
tableDto.getColumns().forEach(column -> {
String simTableName = tableDto.getTableName();
String simTitle = column.getTitle();
List<String> referencedCodeList = tableColumnWithCodeListMap.get(simTableName + "_" + simTitle);
column.setReferencedKpis(referencedCodeList);
}));
}
```
阅读全文