常用的添加树状图层级方法

获取指定分类的上级分类

 获取所有的指定分类的上级分类,不包含自己

    public List<LecturerClassification> findParent(LecturerClassification lecturerClassification, List<LecturerClassification> parentList) {
        LecturerClassification hhyContentTypeTree = lecturerClassificationRepository.findByIdAndDeletedIsFalse(lecturerClassification.getPid());
        if (!ObjectUtils.isEmpty(hhyContentTypeTree)) {
            parentList.add(hhyContentTypeTree);
            if (hhyContentTypeTree.getPid() != 0) {
                findParent(hhyContentTypeTree, parentList);
            }
        }
        return parentList;
    }

 lecturerClassification   传入想获取上级的分类

parentList  一个空的list<指定的分类类型> 用于返回

获取指定分类的下级分类

获取所有的指定分类的下级分类,不包含自己

private List<LecturerClassification> findChildren(LecturerClassification hhyContentType,List<LecturerClassification> childrenList) {
        List<LecturerClassification> hhyContentTypeTree = lecturerClassificationRepository.findOneByPidAndDeletedIsFalse(hhyContentType.getId());
        if (!org.springframework.util.ObjectUtils.isEmpty(hhyContentTypeTree)) {
            childrenList.addAll(hhyContentTypeTree);
            hhyContentTypeTree.forEach(hhyContentType1 ->{
                findChildren(hhyContentType1, childrenList);
            });
        }
        return childrenList;
    }
hhyContentType   传入想获取上级的分类
childrenList  一个空的list<指定的分类类型> 用于返回

获取树状结构

获取指定list的树状机构,赋予层级

 private List<HhyLecturerClassificationVOV2> treeV2(List<LecturerClassification> hhyContentTypeList) {
        List<HhyLecturerClassificationVOV2> hhyContentTypeAOList = new ArrayList<>();
        int i = 1;
        List<Long> collect = hhyContentTypeList.stream().map(LecturerClassification::getPid).collect(Collectors.toList());
        Long min = ObjectUtils.isEmpty(collect) ? 0L : Collections.min(collect);
        hhyContentTypeList.stream().filter(hhyContentType -> hhyContentType.getPid() == min)
                .map(menu -> {
                    HhyLecturerClassificationVOV2 hhyContentTypeAO = new HhyLecturerClassificationVOV2();
                    BeanUtils.copyProperties(menu,hhyContentTypeAO);
                    hhyContentTypeAO.setLevel(i);  //层级
                    List<HhyLecturerClassificationVOV2> hhyContentTypeAOChildren = setChildrenV2(hhyContentTypeAO, hhyContentTypeList, i);
                    hhyContentTypeAO.setChildrenContentType(hhyContentTypeAOChildren);
                    hhyContentTypeAOList.add(hhyContentTypeAO);  //子集
                    return menu;
                }).sorted(Comparator.comparing(LecturerClassification::getSort)).collect(Collectors.toList());
        return hhyContentTypeAOList;
    }

    private List<HhyLecturerClassificationVOV2> setChildrenV2(HhyLecturerClassificationVOV2 hhyContentTypeAO,
                                                            List<LecturerClassification> all,
                                                            int level) {
        List<HhyLecturerClassificationVOV2> hhyContentTypeAOList = new ArrayList<>();
        int i = level + 1;
        all.stream().filter(hhyContentType -> Objects.equals(hhyContentType.getPid(), hhyContentTypeAO.getId()))
                .map(menu -> {
                    HhyLecturerClassificationVOV2 hhyContentTypeAO1 = new HhyLecturerClassificationVOV2();
                    BeanUtils.copyProperties(menu,hhyContentTypeAO1);
                    hhyContentTypeAO1.setLevel(i);
                    List<HhyLecturerClassificationVOV2> hhyContentTypeAOChildren = setChildren(hhyContentTypeAO1, all, i);
                    hhyContentTypeAO1.setChildrenContentType(hhyContentTypeAOChildren);
                    hhyContentTypeAOList.add(hhyContentTypeAO1);
                    return menu;
                }).sorted(Comparator.comparing(LecturerClassification::getSort)).collect(Collectors.toList());
        return hhyContentTypeAOList;
    }
1.treeV2  的传入值
hhyContentTypeList  向组成树状形式的所有分类的lsit集合

2.setChildrenV2  的传入值
hhyContentTypeAO  传入的某个父类
all  向组成树状形式的所有分类的lsit集合
int  层级

获取树状图的层级关系

获取指定树状图的层级数List

// 获取树状图的层级关系
    private List<Integer> maxDeep(List<HhyLecturerClassificationVOV2> tree, List<Integer> list) {
        if (org.springframework.util.ObjectUtils.isEmpty(tree)) {
            return null;
        }
        for (HhyLecturerClassificationVOV2 hhyContentTypeAO : tree) {
            list.add(hhyContentTypeAO.getLevel());
            maxDeep(hhyContentTypeAO.getChildrenContentType(),list);
        }
        return list;
    }
tree 指定的树状图
list  用于返回层级lsit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值