Java8 Stream流处理树形结构数据 方式一

本文介绍了如何使用Java8的StreamAPI和递归来处理树形结构数据,通过模拟查询数据库获取数据,并利用Stream流过滤和映射操作构建完整的树形结构输出。
摘要由CSDN通过智能技术生成

参考资料

  1. Java8新特性-使用Stream流递归实现遍历树形结构


一. 实体类

import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@Builder
public class Menu {
	// 菜单id
    public Integer id;
	// 菜单名称
    public String name;
	// 菜单父级id
    public Integer parentId;
	// 菜单子级id列表
    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;
    }
}

二. 模拟查询树形结构数据

  • ID为2,6,11的Menu 是 ID为1的Menu子节点
  • ID为3,4,5的Menu 是 ID为2的Menu子节点
private List<Menu> queryDataFromDB() {
		
		// 模拟从数据库查询得到数据
        return Arrays.asList(

                new Menu(1, "根节点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", 8),
                new Menu(10, "子节点2.2.2", 8),

                new Menu(11, "子根节点3", 1),
                new Menu(12, "子节点3.1", 11),

                new Menu(13, "根节点2", 0),

                new Menu(14, "子根节点1", 13),
                new Menu(15, "子根节点2", 13)
        );
}

三. 使用stream流处理为树形结构

public void run(String... args) {

    List<Menu> menuList = this.queryDataFromDB();

    List<Menu> resultList = menuList.stream()
    		// 过滤出根节点
            .filter(menu -> menu.getParentId() == 0)
            .peek(menu -> menu.setChildList(this.getChildList(menu, menuList)))
            .collect(Collectors.toList());
	
	// 🧐最终得到树形结构数据
    System.out.println(resultList);
}

private List<Menu> getChildList(Menu rootMenu, List<Menu> menuList) {
    return menuList.stream()
    		// 过滤出ParentId和指定的id相等的数据
            .filter(menu -> Objects.equals(menu.getParentId(), rootMenu.getId()))
            // 递归调用
            .peek(menu -> menu.setChildList(this.getChildList(menu, menuList)))
            .collect(Collectors.toList());
}

💥注意

// ...
.peek(menu -> menu.setChildList(this.getChildList(menu, menuList)))
// ...

是下面这种写法的一种更简单的写法

// ...
.map(menu -> {
    menu.setChildList(this.getChildList(menu, menuList));
    return menu;
})
// ...

四. 处理完的树形结构数据

[
    {
        "id": 1,
        "name": "根节点1",
        "parentId": 0,
        "childList": [
            {
                "id": 2,
                "name": "子根节点1",
                "parentId": 1,
                "childList": [
                    {
                        "id": 3,
                        "name": "子节点1.1",
                        "parentId": 2,
                        "childList": []
                    },
                    {
                        "id": 4,
                        "name": "子节点1.2",
                        "parentId": 2,
                        "childList": []
                    },
                    {
                        "id": 5,
                        "name": "子节点1.3",
                        "parentId": 2,
                        "childList": []
                    }
                ]
            },
            {
                "id": 6,
                "name": "子根节点2",
                "parentId": 1,
                "childList": [
                    {
                        "id": 7,
                        "name": "子节点2.1",
                        "parentId": 6,
                        "childList": []
                    },
                    {
                        "id": 8,
                        "name": "子节点2.2",
                        "parentId": 6,
                        "childList": [
                            {
                                "id": 9,
                                "name": "子节点2.2.1",
                                "parentId": 8,
                                "childList": []
                            },
                            {
                                "id": 10,
                                "name": "子节点2.2.2",
                                "parentId": 8,
                                "childList": []
                            }
                        ]
                    }
                ]
            },
            {
                "id": 11,
                "name": "子根节点3",
                "parentId": 1,
                "childList": [
                    {
                        "id": 12,
                        "name": "子节点3.1",
                        "parentId": 11,
                        "childList": []
                    }
                ]
            }
        ]
    },
    {
        "id": 13,
        "name": "根节点2",
        "parentId": 0,
        "childList": [
            {
                "id": 14,
                "name": "子根节点1",
                "parentId": 13,
                "childList": []
            },
            {
                "id": 15,
                "name": "子根节点2",
                "parentId": 13,
                "childList": []
            }
        ]
    }
]
  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值