zoukankan      html  css  js  c++  java
  • 使用Stream流递归 组合树形结构

    有一些需求,比如构建菜单,构建树形结构,数据库一般就使用父id来表示,为了降低数据库的查询压力,我们可以一次性把数据查出来,然后使用Java8中的Stream流通过流式处理

    实体类:Menu.java

    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<Menu> childList;
    
    
        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<Menu> childList) {
            this.id = id;
            this.name = name;
            this.parentId = parentId;
            this.childList = childList;
        }
    
    }

    递归组装树形结构:

    import java.util.Arrays;
    import java.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;
    
    public class Test {
    
        public static void main(String[] args) {
            testtree();
        }
    
        public static void testtree() {
            //模拟从数据库查询出来
            List<Menu> 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<Menu> collect = menus.stream()
                    .filter(m -> m.getParentId() == 0)
                    .peek(m -> m.setChildList(getChildrens(m, menus)))
                    .collect(Collectors.toList());
            System.out.println(collect.toString());
        }
    
        /**
         * 递归查询节点
         *
         * @param root 节点
         * @param all  所有节点
         * @return 节点信息
         */
        private static List<Menu> getChildrens(Menu root, List<Menu> all) {
            return all.stream()
                    .filter(m -> Objects.equals(m.getParentId(), root.getId()))
                    .peek(m -> m.setChildList(getChildrens(m, all)))
                    .collect(Collectors.toList());
        }
    
    }

    结果:

     文章参考:https://blog.csdn.net/qq_19244927/article/details/106481777

  • 相关阅读:
    九度 1363 欢乐斗地主
    九度 1377 缓变序列
    九度 1376 最近零子序列
    转几篇关于linux下AT&T汇编的帖子
    九度 1358 陈博的平均主义
    九度 1394 五连击数组
    HDU 2817 A sequence of numbers
    HDU 1867 A + B for you again
    HDU 1753 大明A+B
    HDU 1715 大菲波数
  • 原文地址:https://www.cnblogs.com/ooo0/p/14581059.html
Copyright © 2011-2022 走看看