使用stream流获取树形结构数据
结果图

新建控制层进行测试
| import lombok.Data; |
| import org.springframework.web.bind.annotation.GetMapping; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.RestController; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.stream.Collectors; |
| |
| |
| |
| |
| |
| |
| |
| @RestController |
| @RequestMapping("/stream") |
| public class StreamGetTreeTest { |
| |
| } |
结构实体类
| @Data |
| public class Node{ |
| |
| private int id; |
| |
| private int pid; |
| |
| private String name; |
| private List<Node> children = new ArrayList<>(); |
| public Node(int id, int pid, String name) { |
| this.id = id; |
| this.pid = pid; |
| this.name = name; |
| } |
| } |
构建初始化数据方法
| |
| |
| |
| private List<Node> getNodeList() { |
| List<Node> list = new ArrayList<Node>(); |
| Node node1 = new Node(1, 0, "公司库"); |
| Node node2 = new Node(2, 0, "基金库"); |
| Node node3 = new Node(111, 1, "A股市场"); |
| Node node4 = new Node(112, 1, "港股市场"); |
| Node node5 = new Node(211, 2, "公墓基金池"); |
| Node node6 = new Node(212, 2, "非公墓基金池"); |
| Node node7 = new Node(11111, 111, "基础池"); |
| Node node8 = new Node(21211, 212, "可买池"); |
| list.add(node1); |
| list.add(node2); |
| list.add(node3); |
| list.add(node4); |
| list.add(node5); |
| list.add(node6); |
| list.add(node7); |
| list.add(node8); |
| return list; |
| } |
获取树形结构请求方法
| |
| |
| |
| |
| |
| @GetMapping("/getTree") |
| public List<Node> getTree() { |
| List<Node> nodeList = getNodeList(); |
| List<Node> list = nodeList.stream() |
| .filter(node -> 0 == node.getPid()) |
| .map(node -> { |
| node.setChildren(streamPeekGetTree(node, nodeList)); |
| return node; |
| }) |
| .collect(Collectors.toList()); |
| System.out.println(list); |
| return list; |
| } |
流peek得到树
| |
| |
| |
| |
| |
| |
| |
| private List<Node> streamPeekGetTree(Node root, List<Node> allNodeList) { |
| List<Node> childNodeList = allNodeList.stream() |
| .filter(node -> node.getPid() == root.id) |
| .peek(node -> |
| node.setChildren(streamPeekGetTree(node, allNodeList)) |
| ) |
| .collect(Collectors.toList()); |
| return childNodeList; |
| } |
运行效果

完整代码
| package com.goods.controller; |
| |
| import lombok.Data; |
| import org.springframework.web.bind.annotation.GetMapping; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.RestController; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.stream.Collectors; |
| |
| |
| |
| |
| |
| |
| |
| @RestController |
| @RequestMapping("/stream") |
| public class StreamGetTreeTest { |
| @Data |
| public class Node{ |
| |
| private int id; |
| |
| private int pid; |
| |
| private String name; |
| private List<Node> children = new ArrayList<>(); |
| public Node(int id, int pid, String name) { |
| this.id = id; |
| this.pid = pid; |
| this.name = name; |
| } |
| } |
| |
| |
| |
| private List<Node> getNodeList() { |
| List<Node> list = new ArrayList<Node>(); |
| Node node1 = new Node(1, 0, "公司库"); |
| Node node2 = new Node(2, 0, "基金库"); |
| Node node3 = new Node(111, 1, "A股市场"); |
| Node node4 = new Node(112, 1, "港股市场"); |
| Node node5 = new Node(211, 2, "公墓基金池"); |
| Node node6 = new Node(212, 2, "非公墓基金池"); |
| Node node7 = new Node(11111, 111, "基础池"); |
| Node node8 = new Node(21211, 212, "可买池"); |
| list.add(node1); |
| list.add(node2); |
| list.add(node3); |
| list.add(node4); |
| list.add(node5); |
| list.add(node6); |
| list.add(node7); |
| list.add(node8); |
| return list; |
| } |
| |
| |
| |
| |
| |
| |
| @GetMapping("/getTree") |
| public List<Node> getTree() { |
| List<Node> nodeList = getNodeList(); |
| List<Node> list = nodeList.stream() |
| .filter(node -> 0 == node.getPid()) |
| .map(node -> { |
| node.setChildren(streamPeekGetTree(node, nodeList)); |
| return node; |
| }) |
| .collect(Collectors.toList()); |
| System.out.println(list); |
| return list; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| private List<Node> streamPeekGetTree(Node root, List<Node> allNodeList) { |
| List<Node> childNodeList = allNodeList.stream() |
| .filter(node -> node.getPid() == root.id) |
| .peek(node -> |
| node.setChildren(streamPeekGetTree(node, allNodeList)) |
| ) |
| .collect(Collectors.toList()); |
| return childNodeList; |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下