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

发布于:2023-01-12 ⋅ 阅读:(601) ⋅ 点赞:(0)

使用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())
            .map(node -> {
                node.setChildren(streamPeekGetTree(node, nodeList));
                return node;
            })
            .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;
    }
}

本文含有隐藏内容,请 开通VIP 后查看