zoukankan      html  css  js  c++  java
  • 不用递归实现List转Tree

    缘由:能不递归就不递归

    代码实现Demo

    import com.alibaba.fastjson.JSON;
    import org.springframework.util.CollectionUtils;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class ListToTreeDemo {
    
        private static class Node {
            private int id;
            private int pid;
            private String name;
            private List<Node> child;
    
            public Node(int id, int pid) {
                this.id = id;
                this.pid = pid;
                this.name = "测试:"+ pid + ":" + id;
            }
    
            public int getId() {
                return id;
            }
    
            public void setId(int id) {
                this.id = id;
            }
    
            public int getPid() {
                return pid;
            }
    
            public void setPid(int pid) {
                this.pid = pid;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public List<Node> getChild() {
                return child;
            }
    
            public void setChild(List<Node> child) {
                this.child = child;
            }
        }
    
        public static void main(String[] args) {
    
            List<Node> nodes = new ArrayList<>();
            nodes.add(new Node(1, 0));
            nodes.add(new Node(2, 0));
            nodes.add(new Node(3, 0));
            nodes.add(new Node(4, 0));
            nodes.add(new Node(5, 1));
            nodes.add(new Node(6, 1));
            nodes.add(new Node(7, 2));
            nodes.add(new Node(8, 5));
            Map<Integer, List<Node>> collect = nodes.stream().collect(Collectors.groupingBy(Node::getPid));
    
            nodes.forEach((item) -> {
                if (CollectionUtils.isEmpty(collect.get(item.getId()))) {
                    item.setChild(new ArrayList<>());
                } else {
                    item.setChild(collect.get(item.getId()));
                }
            });
            List<Node> list = nodes
                    .stream()
                    .filter((item) -> item.getPid() == 0)
                    .collect(Collectors.toList());
    
            System.out.println(JSON.toJSONString(list));
        }
    }
    
    有三个字送给你,
    一是“诚”,
    二是“勤”,
    三是“专”。
    当你无比地想做成一件事,
    愿意为它倾尽无数心血和努力时,
    结果总不会太差。
  • 相关阅读:
    C++ 重载操作符- 01 简单的入门
    C++ 析构函数
    Auto Control 001 自动控制的一般概念
    C++ 友元
    安装 SQL Server 2014 Express
    关闭是否只查看安全传送的网页内容提示框 和 是否允许运行软件,如ActiveX控件和插件提示框
    Python 网络爬虫 010 (高级功能) 解析 robots.txt 文件
    2019-06-12 Java学习日记之JDBC
    2019-06-11 Java学习日记之Bootstrap
    2019-06-10 Java学习日记之JQuery
  • 原文地址:https://www.cnblogs.com/jarjune/p/14605487.html
Copyright © 2011-2022 走看看