• 将数据库的List转化为Tree


    将数据库的List转化为Tree

    参考

    java—将数据库读取的list转tree

    package com.mozq.common.date;
    
    import lombok.Data;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;
    
    @Data
    public class DeptNode {
        private Integer id;
        private Integer parentId;
        private String name;
        private List<DeptNode> children;
    
        public static List<DeptNode> getTreeFor(List<DeptNode> list, Integer rootId){
            List<DeptNode> roots = new ArrayList<>();
            //children的值不设置为null
            list.forEach(e->e.setChildren(new ArrayList<>()));
    
            for (DeptNode deptNode : list) {
                if(deptNode.getParentId().equals(rootId)){
                    roots.add(deptNode);
                }
                for (DeptNode node : list) {
                    //寻找父子关系,为存在关系的建立关系
                    if(deptNode.getParentId().equals(node.getId())){
                        if(Objects.isNull(node.getChildren())){
                            node.setChildren(new ArrayList<>());
                        }
                        node.getChildren().add(deptNode);
                    }
                }
            }
            return roots;
        }
        public static List<DeptNode> getTree(List<DeptNode> list, Integer rootId){
            if(Objects.isNull(list) || list.isEmpty()){
                return new ArrayList<>();
            }
            //获取根列表
            List<DeptNode> roots = list.stream().filter(e -> e.getParentId().equals(rootId)).collect(Collectors.toList());
            if(roots.isEmpty()){
                //return null;
                return new ArrayList<>();
            }
            //为根设置子元素
            for (DeptNode root : roots) {
                List<DeptNode> children = getTree(list, root.getId());
                root.setChildren(children);
            }
            return roots;
        }
    
        public static void main(String[] args) {
            List<DeptNode> tree = DeptNode.getTree(data(), 0);
            System.out.println(tree);
            List<DeptNode> treeFor = DeptNode.getTreeFor(data(), 0);
            System.out.println(treeFor);
        }
    
        private static List<DeptNode> data(){
            DeptNode d1 = new DeptNode();
            d1.setName("haha");
            d1.setId(1);
            d1.setParentId(0);
            DeptNode d2 = new DeptNode();
            d2.setName("腾讯");
            d2.setId(2);
            d2.setParentId(0);
    
            DeptNode d11 = new DeptNode();
            d11.setName("研发");
            d11.setId(11);
            d11.setParentId(1);
    
            DeptNode d12 = new DeptNode();
            d12.setName("测试");
            d12.setId(12);
            d12.setParentId(1);
    
            List<DeptNode> deptNodeList = new ArrayList<>();
            deptNodeList.add(d1);
            deptNodeList.add(d2);
            deptNodeList.add(d11);
            deptNodeList.add(d12);
            return deptNodeList;
        }
    }
    
  • 相关阅读:
    Linux免密登录
    HDFS shell 常用命令
    zabbix4.4图表中文显示乱码解决办法
    安装zabbix-agent
    安装配置zabbix4.4
    elasticsearch插件sql安装
    dedecms调用头部文件 dede:include时页面出现一行空白的解决方案
    DedeCMS <=5.7 SP2 file_class.php 任意文件上传漏洞
    DedeCMS后台文件任意上传漏洞media_add.php的修改方法
    织梦DEDECMS任意文件上传漏洞与注入漏洞修复方法
  • 原文地址:https://www.cnblogs.com/mozq/p/12316312.html
Copyright © 2020-2023  润新知