组建树状数据结构算法

前言

提示:平铺数据转为层级树状数据结构


一、代码如下:

		//查询数据源信息
        List<JyeooSectionPo> jyeooSectionPos = getJyeooSectionPos(bookId);
        if (!CollectionUtils.isEmpty(jyeooSectionPos)) {
            //2.组装成知识点树
            List<JyeooSectionTreeOutVo> yunhenPointVos = new ArrayList<>();
            if (CollectionUtils.isEmpty(jyeooSectionPos)) {
                return ResultUtil.createResult(true, "查询成功!", yunhenPointVos);
            }
            if (!CollectionUtils.isEmpty(jyeooSectionPos)) {
                for (JyeooSectionPo yunhenPointPo : jyeooSectionPos) {
                    JyeooSectionTreeOutVo yunhenPointVo = new JyeooSectionTreeOutVo();
                    BeanUtil.copyProperties(yunhenPointPo, yunhenPointVo);
                    yunhenPointVos.add(yunhenPointVo);
                }
            }
            //获取第一层知识点
            List<JyeooSectionTreeOutVo> firstLevelPoints = new ArrayList<>();
            for (JyeooSectionTreeOutVo yunhenPointVo : yunhenPointVos) {
                String pid = yunhenPointVo.getPid();
                if (StringUtils.isEmpty(pid)) {
                    firstLevelPoints.add(yunhenPointVo);
                    List<JyeooSectionTreeOutVo> subPointTree = getPointTree(yunhenPointVos, yunhenPointVo.getId());
                    yunhenPointVo.setChildren(subPointTree);
                }
                //可以排序
            }
            return ResultUtil.createResult(true, "查询成功!", firstLevelPoints);
        }
 /**
     * @Description: 将平级的维度列表,构造成树形结构的维度信息
     * @Param: [yunhenPointVos, id]
     * @Return: List<DimVo>
     * @Author: gx
     * @CreateDate 
     * @UpdateDate 
     */
    public List<JyeooSectionTreeOutVo> getPointTree(List<JyeooSectionTreeOutVo> yunhenPointVos, String id) {
        if (yunhenPointVos == null || yunhenPointVos.size() == 0) {
            return null;
        }
        List<JyeooSectionTreeOutVo> list = new ArrayList<>();
        List<JyeooSectionTreeOutVo> listContinue = new ArrayList<>(yunhenPointVos);

        for (JyeooSectionTreeOutVo yunhenPointVo : yunhenPointVos) {
            if (id.equals(yunhenPointVo.getPid())) {
                listContinue.remove(yunhenPointVo);
                yunhenPointVo.setChildren(getPointTree(listContinue, yunhenPointVo.getId()));
                list.add(yunhenPointVo);
            }
        }
        if (list.size() == 0) {
            return null;
        }
        return list;
    }

2.JyeooSectionTreeOutVo实体

代码如下(示例):

package com.yunhenedu.homework.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
public class JyeooSectionTreeOutVo implements Cloneable, Serializable {
    /**
     *   主键
     */
    private String id;

    /**
     *   菁优id
     */
    private String jyeooId;

    /**
     *   父菁优id
     */
    private String pjyeooId;

    /**
     *   父id
     */
    private String pid;

    /**
     *   排序号
     */
    private String seq;

    /**
     *   章节名
     */
    private String name;

    /**
     *   描述
     */
    private String describes;

    /**
     *   版本详情主键
     */
    private String editionDetailId;

    /**
     *   版本详情菁优id
     */
    private String editionDetailJyeooId;

    /**
     *   学科英文名
     */
    private String subjectEn;

    @ApiModelProperty("子知识点列表")
    private List<JyeooSectionTreeOutVo> children;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值