tree树型结构统计每个层级的总数

1、如下图所示红色框框里面的数字如何统计出来

条件:

       已知条件为最下一个层级的总数

答:

1、获取所有分类信息列表

2、根据分类信息列表获取最下一级商品总数

3、反向累加子级数据到父级

int counts = 0;
List<TreeVO> treeVos = new ArrayList<>();
TreeVO treeVo = new TreeVO(0, "全部", "#", "fa fa-folder-o", "#", null, false, true, true, counts, d);
treeVos.add(treeVo);


for (ProductTypePo p : productTypes) {
    TreeVO tree = null;
    Map<String, Object> attr = new HashMap<>();
    attr.put("level", p.getClassLvl());
    attr.put("edit", true);
    int count = 0;
    if (request.isCountProduct()) {
        // 组装模糊匹配key
        count = null == countMap.get(String.valueOf(p.getClassId())) ? 0 : countMap.get(String
                .valueOf(p.getClassId()));
        counts += count;
        attr.put("count", count);
    }
    if (p.getClassLvl().intValue() == 4) {
        attr.put("child", false);
        tree = new TreeVO(p.getClassId(), p.getClassNm(), p.getfClassId(), "fa fa-folder-o",
                p.getClassLvl(), p.getClassType(), false, false, false, count, attr);
    } else {
        attr.put("child", true);
        tree = new TreeVO(p.getClassId(), p.getClassNm(), p.getfClassId(), "fa fa-folder-o",
                p.getClassLvl(), p.getClassType(), false, false, false, count, attr);
    }
    treeVos.add(tree);
}
d.put("count", counts);


Integer levl = 4;//最高层级
boolean flag = true;
while(flag){
    levl-=1;
    List<TreeVO> treeVoLst = new ArrayList<>();
    for (TreeVO tree : treeVos) {
        if(tree.getData().get("level").equals(levl)){
            treeVoLst.add(tree);
        }
    }
    totTreeCount(treeVos,treeVoLst);
    if(levl==0)
        flag = false;
}


private boolean totTreeCount(List<TreeVO> treeVos, List<TreeVO> treeVoLst) {
        for (TreeVO treeVO1 : treeVoLst) {
            int count = 0;
            for (TreeVO treeVO2 : treeVos) {
                if (treeVO1.getId().equals(treeVO2.getParent())) {
                    count += treeVO2.getCount();
                }
            }
            treeVO1.setCount(count);
            treeVO1.getData().put("count", count);
        }
        return true;
    }




  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值