使用Java 8的Stream轻松遍历树形结构

1 场景

平时会遇到一些常规需求,比如构建菜单、构建树形结构,在数据库中一般使用父ID实现单表存储,为了降低数据库的查询压力,可以将数据一次性从数据库中查询出来,然后使用Java 8的Stream流处理。

2 实体类

/**
 * Menu
 *
 * @author Jin Zhenlin
 * @date 2022/9/13 16:46
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonPropertyOrder({"name", "childList", "id", "parentId"})
public class Menu {

    /**
     * id
     */
    private Integer id;

    /**
     * 名称
     */
    private String name;

    /**
     * 父级ID,根节点的父级ID为0
     */
    private Integer parentId;

    /**
     * 子节点列表信息
     */
    private List<Menu> childList;

    public Menu(Integer id, String name, Integer parentId) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
    }
}

3 递归组装树形结构

    @Test
    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> menuTree = menus.stream()
                .filter(menu -> {
                    return menu.getParentId() == 0;
                })
                .peek(menu -> {
                    menu.setChildList(getChildren(menu, menus));
                })
                .collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(menuTree));
    }

    /**
     * 递归查询子节点数据
     *
     * @param root 根节点
     * @param all  所有节点
     * @return 子节点数据
     */
    private List<Menu> getChildren(Menu root, List<Menu> all) {
        List<Menu> children = all.stream()
                .filter(menu -> {
                    return root.getId().equals(menu.getParentId());
                })
                .peek(menu -> {
                    menu.setChildList(getChildren(menu, all));
                })
                .collect(Collectors.toList());
        return children;
    }

4 输出结果

[
    {
        "name":"根节点",
        "childList":[
            {
                "name":"子节点1",
                "childList":[
                    {
                        "name":"子节点1.1",
                        "childList":[

                        ],
                        "id":3,
                        "parentId":2
                    },
                    {
                        "name":"子节点1.2",
                        "childList":[

                        ],
                        "id":4,
                        "parentId":2
                    },
                    {
                        "name":"子节点1.3",
                        "childList":[

                        ],
                        "id":5,
                        "parentId":2
                    }
                ],
                "id":2,
                "parentId":1
            },
            {
                "name":"子节点2",
                "childList":[
                    {
                        "name":"子节点2.1",
                        "childList":[
                            {
                                "name":"子节点2.2.1",
                                "childList":[

                                ],
                                "id":9,
                                "parentId":7
                            },
                            {
                                "name":"子节点2.2.2",
                                "childList":[

                                ],
                                "id":10,
                                "parentId":7
                            }
                        ],
                        "id":7,
                        "parentId":6
                    },
                    {
                        "name":"子节点2.2",
                        "childList":[

                        ],
                        "id":8,
                        "parentId":6
                    }
                ],
                "id":6,
                "parentId":1
            },
            {
                "name":"子节点3",
                "childList":[
                    {
                        "name":"子节点3.1",
                        "childList":[

                        ],
                        "id":12,
                        "parentId":11
                    }
                ],
                "id":11,
                "parentId":1
            }
        ],
        "id":1,
        "parentId":0
    }
]
posted @ 2022-09-13 17:21  超级鲨鱼辣椒  阅读(1050)  评论(0编辑  收藏  举报