论算法重要性 递归返回值null

文章目录
  1. 1. 问题分析
  2. 2. 解决方法

遇到一个树形结构的数据处理,需要用到递归算法,想想也简单,就写呗,经过调试,一旦进入递归返回值就被清空了

问题分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private Map<String,BigDecimal> getAbnormalsLevel(String checkId, Integer itemId,Map<String,BigDecimal> levelMap) {
List<String> list = tZhiKangCheckMapper.selectCheckDataByFatherItemId(checkId,itemId);
if(list.isEmpty()){
//查不到数据 说明没有子节点了;
Map<String,Object> result = tZhiKangCheckMapper.selectAbnormalsLevel(checkId,itemId);
Assert.notNull(result, "查询异常数据错误");

BigDecimal abnormalsNewCount = new BigDecimal(result.get("abnormalsCount").toString());
BigDecimal abnormalsNewLevel = new BigDecimal(result.get("abnormalsLevel").toString());

if(levelMap==null){
levelMap = new HashMap<>();
BigDecimal abnormalsCount = BigDecimal.ZERO;
BigDecimal abnormalsLevel = BigDecimal.ZERO;
levelMap.put("abnormalsCount",abnormalsCount);
levelMap.put("abnormalsLevel",abnormalsLevel);
}

BigDecimal abnormalsCount = new BigDecimal(levelMap.get("abnormalsCount").toString());
BigDecimal abnormalsLevel = new BigDecimal(levelMap.get("abnormalsLevel").toString());

levelMap.put("abnormalsCount",abnormalsCount.add(abnormalsNewCount));
levelMap.put("abnormalsLevel",abnormalsLevel.add(abnormalsNewLevel));
}else{
//查到数据递归继续遍历
for(String fatherItemIdStr : list){
Integer fatherItemId = Integer.valueOf(fatherItemIdStr);
getAbnormalsLevel(checkId,fatherItemId,levelMap);
}
}
return levelMap;
}

当有子节点的时候进入循环递归,进入后levelMap的值就被清空了,没有达到累加效果

解决方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
private Map<String,BigDecimal> getAbnormalsLevel(String checkId, Integer itemId,Map<String,BigDecimal> levelMap) {
List<String> list = tZhiKangCheckMapper.selectCheckDataByFatherItemId(checkId,itemId);
if(list.isEmpty()){
//查不到数据 说明没有子节点了;
Map<String,Object> result = tZhiKangCheckMapper.selectAbnormalsLevel(checkId,itemId);
Assert.notNull(result, "查询异常数据错误");

BigDecimal abnormalsNewCount = new BigDecimal(result.get("abnormalsCount").toString());
BigDecimal abnormalsNewLevel = new BigDecimal(result.get("abnormalsLevel").toString());

if(levelMap==null){
levelMap = new HashMap<>();
BigDecimal abnormalsCount = BigDecimal.ZERO;
BigDecimal abnormalsLevel = BigDecimal.ZERO;
levelMap.put("abnormalsCount",abnormalsCount);
levelMap.put("abnormalsLevel",abnormalsLevel);
}

BigDecimal abnormalsCount = new BigDecimal(levelMap.get("abnormalsCount").toString());
BigDecimal abnormalsLevel = new BigDecimal(levelMap.get("abnormalsLevel").toString());

levelMap.put("abnormalsCount",abnormalsCount.add(abnormalsNewCount));
levelMap.put("abnormalsLevel",abnormalsLevel.add(abnormalsNewLevel));
}else{
//查到数据递归继续遍历
for(String fatherItemIdStr : list){
Integer fatherItemId = Integer.valueOf(fatherItemIdStr);
levelMap = getAbnormalsLevel(checkId,fatherItemId,levelMap);
}
}

return levelMap;
}

[u]将递归后的值重新赋给返回值即可,就这么个小毛病折腾了好久[/u]

1
levelMap = getAbnormalsLevel(checkId,fatherItemId,levelMap);
评论