Java分组返回树形数据

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。Java分组返回树形数据,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

/**
 * @date 2023/10/25
 * @description
 */

import com.alibaba.fastjson2.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: 工具箱
 * @description 返回树形数据
 * @date 2023/9/15
 */
public class T1 {
    static class Node {
        Integer id;
        Integer pid;
        String name;

        List<Node> treeNode = new ArrayList<>();

        public List<Node> getTreeNode() {
            return treeNode;
        }

        public void setTreeNode(List<Node> treeNode) {
            this.treeNode = treeNode;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public Integer getPid() {
            return pid;
        }

        public void setPid(Integer pid) {
            this.pid = pid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Node(Integer id, Integer pid, String name) {
            this.id = id;
            this.pid = pid;
            this.name = name;
        }

        public Node() {
        }

        @Override
        public String toString() {
            return "Node{" +
                    "id=" + id +
                    ", pid=" + pid +
                    ", name='" + name + '\'' +
                    ", treeNode=" + treeNode +
                    '}';
        }
    }

    /**
     * 实现方式1,儿子为空不显示
     */
    public static void main1() {
        long l1 = System.currentTimeMillis();
        Node A = new Node(1, 0, "A");
        Node B = new Node(2, 1, "B");
        Node C = new Node(3, 2, "C");
        Node B1 = new Node(4, 1, "B1");

        Node C1 = new Node(5, 0, "C1");
        // 组装树状结构
        List<Node> nodes = Arrays.asList(A, B, C, B1, C1);

        //将这些非顶级节点的数据按pid进行分组,这个是根据pid为key
        //第一步过滤非pid=0的节点
        //第二步进行分组
        Map<Integer, List<Node>> nodeMap = nodes.stream().filter(node -> node.getPid() != 0).collect(Collectors.groupingBy(node -> node.getPid()));

        //循环设置对应的子节点(根据id=pid)上一步以pid为key所以就直接循环获取
        nodes.forEach(node -> node.setTreeNode(nodeMap.get(node.getId())));

        //过滤第一层是pid为零的数据,也就是没有根节点的数据
        List<Node> collect = nodes.stream().filter(node -> node.getPid() == 0).collect(Collectors.toList());
        System.out.println(JSON.toJSON(collect));
        long l2 = System.currentTimeMillis();
        long l = l2 - l1;
        System.out.println(l);
    }

    /**
     * 实现方式2,儿子为空也显示
     */
    public static void main2() {
        long l1 = System.currentTimeMillis();
        Node A = new Node(1, 0, "A");
        Node B = new Node(2, 1, "B");
        Node C = new Node(3, 2, "C");
        Node B1 = new Node(4, 1, "B1");

        Node C1 = new Node(5, 0, "C1");
        // 组装树状结构
        List<Node> nodes = Arrays.asList(A, B, C, B1, C1);

        //将这些非顶级节点的数据按pid进行分组,这个是根据pid为key
        //第一步构造最外层节点,即id=0的节点
        List<Node> collect = nodes.stream()
                .filter(node -> node.getPid() == 0)
                .peek(node -> {
                    node.setTreeNode(getChildren(node, nodes));//id=0的节点就为他设置孩子节点
                }).
                collect(Collectors.toList());
        System.out.println(JSON.toJSON(collect));
        long l2 = System.currentTimeMillis();
        long l = l2 - l1;
        System.out.println(l);
    }

    static List<Node> getChildren(Node node, List<Node> nodes) {
        return nodes.stream()
                .filter(item -> item.getPid().equals(node.getId()))//判断当前节点的父id是不是要设置节点的id
                .peek(item -> {
                    item.setTreeNode(getChildren(item, nodes));//如果是 为其设置孩子节点 通过递归 为每个除了最外层节点的节点设置孩子节点
                })
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        main1();
        main2();
    }
}

结果
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/192719.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!