java使用stream流peek方法获取树形结构数据【简单整洁】

使用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;

/**
 * @author wuzhenyong
 * ClassName:StreamGetTreeTest.java
 * date:2022-08-01 16:26
 * Description: 使用stream流获取树形结构数据
 */
@RestController
@RequestMapping("/stream")
public class StreamGetTreeTest {

}

结构实体类

@Data
public class Node{
    // id
    private int id;
    // 父及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;
}

获取树形结构请求方法

/**
* 获取树形结构
 *
 * @return {@link List<Node>}
 */
@GetMapping("/getTree")
public List<Node> getTree() {
    List<Node> nodeList = getNodeList();
    List<Node> list = nodeList.stream()
            .filter(node -> 0 == node.getPid())
            .peek(node -> 
                node.setChildren(streamPeekGetTree(node, nodeList))
            )
            .collect(Collectors.toList());
    System.out.println(list);
    return list;
}

流peek得到树

/**
* 流peek得到树
 *
 * @param root        根节点
 * @param allNodeList 所有节点列表
 * @return {@link List<Node>}
 */
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;

/**
 * @author wuzhenyong
 * ClassName:StreamGetTreeTest.java
 * date:2022-08-01 16:26
 * Description: 使用stream流获取树形结构数据
 */
@RestController
@RequestMapping("/stream")
public class StreamGetTreeTest {
    @Data
    public class Node{
        // id
        private int id;
        // 父及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;
    }

    /**
     * 获取树形结构
     *
     * @return {@link List<Node>}
     */
    @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得到树
     *
     * @param root        根节点
     * @param allNodeList 所有节点列表
     * @return {@link List<Node>}
     */
    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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个小浪吴啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值