• java 8 stream api将List<T>转换成树形结构


    1、新建实体类

    package com.example.springboot3.entity;
    
    import lombok.Builder;
    import lombok.Data;
    
    import java.util.List;
    
    @Data
    @Builder
    public class Menu {
        /**
         * id
         */
        public Integer id;
        /**
         * 名称
         */
        public String name;
        /**
         * 父id ,根节点为0
         */
        public Integer parentId;
        /**
         * 子节点信息
         */
        public List children;
    
    
        public Menu(Integer id, String name, Integer parentId) {
            this.id = id;
            this.name = name;
            this.parentId = parentId;
        }
    
        public Menu(Integer id, String name, Integer parentId, List children) {
            this.id = id;
            this.name = name;
            this.parentId = parentId;
            this.children = children;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    2、controller测试,正式环境可到service进行分层

    package com.example.springboot3.controller;
    
    import com.example.springboot3.entity.Menu;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;
    
    @RestController
    public class MenuController {
    
        @GetMapping("/getMenuTree")
        public List getMenuTree(){
            //模拟从数据库查询出来
            List menus = Arrays.asList(
                    new Menu(1,"根节点",0),
                    new Menu(2,"子节点1",1),
                    new Menu(3,"子节点1.1",2),
                    new Menu(4,"子节点1.2",2),
                    new Menu(5,"根节点1.3",2),
                    new Menu(6,"根节点2",1),
                    new Menu(7,"根节点2.1",6),
                    new Menu(8,"根节点2.2",6),
                    new Menu(9,"根节点2.2.1",7),
                    new Menu(10,"根节点2.2.2",7),
                    new Menu(11,"根节点3",1),
                    new Menu(12,"根节点3.1",11)
            );
    
            //获取父节点
            List collect = menus.stream().filter(m -> m.getParentId() == 0).map(
                    (m) -> {
                        m.setChildren(getChildrens(m, menus));
                        return m;
                    }
            ).collect(Collectors.toList());
            return collect;
        }
    
        private List getChildrens(Menu root, List all) {
            List children = all.stream().filter(m ->
                    Objects.equals(m.getParentId(), root.getId())
            ).map(
                    (m) -> {
                        m.setChildren(getChildrens(m, all));
                        return m;
                    }
            ).collect(Collectors.toList());
            return children;
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    3、结果

    在这里插入图片描述

  • 相关阅读:
    flink理论干货笔记(2)
    豆瓣图书评分数据的可视化分析
    Linux多线程C++版(八) 线程同步方式-----条件变量
    FFmpeg与其他库的交互
    LeetCode59.螺旋矩阵
    【Unity3D】拖尾TrailRenderer
    Redis面试连环问,你能撑到哪一问
    Linux下C语言使用 netlink sockets与内核模块通信
    人工智能 多元线性回归(一)
    spring actuator未授权之heapdump自动化利用【JDumpSpider】
  • 原文地址:https://blog.csdn.net/weixin_41463944/article/details/137077154