向下递归以及向上递归

向下递归以及向上递归

结果以json格式输出,可以用json在线解析,方便查看

package com.xintone.demo;

import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class Recursion {
    // 主键id
    private Integer id;
    // 父级id
    private Integer parentId;
    // 子集
    private List<Recursion> children;
    // 层级
    private Integer level;

    public Recursion(Integer id, Integer parentId) {
        this.id = id;
        this.parentId = parentId;
    }

    public static void main(String[] args) {
        // 获取测试数据
        List<Recursion> recursions1 = getList();
        // 获取顶级父集
        List<Recursion> parents = recursions1.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList());
        // 设置层级
        parents.forEach(item -> item.setLevel(0));
        // 向下递归
        downwardRecursion(parents, recursions1);
        System.out.println("向下递归:" + JSONUtil.toJsonStr(parents));

        System.out.println("-------------分割线-------------");
        // 获取测试数据
        List<Recursion> recursions2 = getList();
        // 获取测试数据中所有的 parentId
        List<Integer> parentIds = recursions2.stream().map(Recursion::getParentId).collect(Collectors.toList());
        // 判断 id 是否在 parentIds 中,不在则是最子级
        List<Recursion> children = recursions2.stream().filter(item -> !parentIds.contains(item.getId())).collect(Collectors.toList());
        // 向上递归
        upwardRecursion(children, recursions2);
        // 递归完从测试数据中筛选出最顶级
        List<Recursion> tree = recursions2.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList());
        System.out.println("向上递归:" + JSONUtil.toJsonStr(tree));
    }

    private static void upwardRecursion(List<Recursion> children, List<Recursion> all) {
        // 遍历子集
        children.forEach(child -> {
            // 获取该子级的父级
            Recursion parent = all.stream().filter(item -> child.getParentId().equals(item.getId())).findFirst().orElse(null);
            // 判断父级是否为空,如果为空则是最顶级
            if (parent != null) {
                // 判断父级的子集是否为空
                if (parent.getChildren() == null) {
                    // 新建一个集合将子级添加到父级的子集中
                    parent.setChildren(new ArrayList<Recursion>() {{
                        add(child);
                    }});
                    // 判断该子级是否在父级的子集中存在
                    // 因为在遍历子级时,会出现多个子级的父级相同,如果不加判断会导致数据重复
                } else if (!parent.getChildren().contains(child)) {
                    // 将该子级添加到父级的子集中
                    parent.getChildren().add(child);
                }
                // 将父级添加到集合并向上递归
                upwardRecursion(new ArrayList<Recursion>() {{
                    add(parent);
                }}, all);
                child.setLevel(parent.getLevel() + 1);
            } else {
                child.setLevel(0);
            }
        });
    }

    private static void downwardRecursion(List<Recursion> parents, List<Recursion> all) {
        // 遍历父集
        parents.forEach(recursion -> {
            // 获取该父级的子集
            List<Recursion> children = all.stream().filter(item -> item.getParentId().equals(recursion.getId())).collect(Collectors.toList());
            // 判断子集是否为空
            if (!CollectionUtils.isEmpty(children)) {
                // 设置子集的层级
                children.forEach(item -> item.setLevel(recursion.getLevel() + 1));
                // 将子集添加到该父级的子集去
                recursion.setChildren(children);
                // 向下递归
                downwardRecursion(children, all);
            }
        });
    }

    private static List<Recursion> getList() {
        List<Recursion> list = new ArrayList<>();
        list.add(new Recursion(1, 0));
        list.add(new Recursion(2, 0));
        list.add(new Recursion(3, 0));
        list.add(new Recursion(4, 1));
        list.add(new Recursion(5, 1));
        list.add(new Recursion(6, 2));
        list.add(new Recursion(7, 2));
        list.add(new Recursion(8, 3));
        list.add(new Recursion(9, 3));
        list.add(new Recursion(10, 4));
        list.add(new Recursion(11, 4));
        list.add(new Recursion(12, 5));
        list.add(new Recursion(13, 5));
        list.add(new Recursion(14, 6));
        list.add(new Recursion(15, 6));
        list.add(new Recursion(16, 7));
        list.add(new Recursion(17, 7));
        list.add(new Recursion(18, 8));
        list.add(new Recursion(19, 8));
        list.add(new Recursion(20, 9));
        list.add(new Recursion(21, 9));
        return list;
    }
}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 向下递归以及向上递归