for (Entity e : rsList) { List<String> singleRecord = new ArrayList<>(); nameList.forEach(n -> singleRecord.add(e.get(n) == null ? "" : e.get(n).toString())); if (e.getMetadata().containsKey(ChargeConst.RATE_RULE_DETAIL)) { Set<Map<String, Object>> ruleDtls = (Set<Map<String, Object>>) e.getMetadata().get(ChargeConst.RATE_RULE_DETAIL); Map<Object, Object> mergedMap = ruleDtls.stream() .flatMap(map -> map.entrySet().stream().map(entry -> new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue() == null ? "" : entry.getValue()))) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> newValue)); final List<String> finalSingleRecord = singleRecord; valueList.add(IntStream.range(0, finalSingleRecord.size()).mapToObj( i -> finalSingleRecord.get(i).equals("") && mergedMap.get(nameList.get(i)) != null ? mergedMap.get(nameList.get(i)).toString() : finalSingleRecord.get(i)) .collect(Collectors.toList())); continue; } valueList.add(singleRecord); }降低代码复杂度
时间: 2023-12-19 12:06:31 浏览: 137
可以将代码分解为多个方法来降低复杂度,例如:
1. 提取一个方法来处理每个实体记录,将其转换为一个 String 类型的 List。
```
private List<String> processEntity(Entity e, List<String> nameList) {
List<String> singleRecord = new ArrayList<>();
nameList.forEach(n -> singleRecord.add(e.get(n) == null ? "" : e.get(n).toString()));
if (e.getMetadata().containsKey(ChargeConst.RATE_RULE_DETAIL)) {
Set<Map<String, Object>> ruleDtls = (Set<Map<String, Object>>) e.getMetadata().get(ChargeConst.RATE_RULE_DETAIL);
Map<Object, Object> mergedMap = mergeRuleDetails(ruleDtls);
final List<String> finalSingleRecord = singleRecord;
return IntStream.range(0, finalSingleRecord.size())
.mapToObj(i -> finalSingleRecord.get(i).equals("") && mergedMap.get(nameList.get(i)) != null ? mergedMap.get(nameList.get(i)).toString() : finalSingleRecord.get(i))
.collect(Collectors.toList());
}
return singleRecord;
}
```
2. 提取一个方法来合并所有规则细节的 Map。
```
private Map<Object, Object> mergeRuleDetails(Set<Map<String, Object>> ruleDtls) {
return ruleDtls.stream()
.flatMap(map -> map.entrySet().stream()
.map(entry -> new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue() == null ? "" : entry.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> newValue));
}
```
3. 将原来的主方法进行简化,使用上面的两个方法来处理每个实体记录,并将结果添加到 valueList 中。
```
for (Entity e : rsList) {
List<String> singleRecord = processEntity(e, nameList);
valueList.add(singleRecord);
}
```
阅读全文
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)