前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >List转换为tree-项目真实使用

List转换为tree-项目真实使用

作者头像
知识浅谈
发布2022-05-10 09:05:11
3010
发布2022-05-10 09:05:11
举报
文章被收录于专栏:分享学习

前提:数据库中查出来每一条数据构成链表,需要我们转换成树结构,id,pid(父节点的id) name

主要代码:class Test -》public Node getRoot(List list) 0为根节点 1-9为次根节点 1.1-1.9 2.1-2.9 … 为次次根节点 看下方测试结果注意只有0的pid为-1

代码语言:javascript
复制
@RestController
public class AAA {
    @RequestMapping("/project/test")
    public Result get123(){
        Test test = new Test();
        List<Node> list = new ArrayList<>();
        list.add(new Node("0","-1","根节点"));
        list.add(new Node("9","0","9"));
        list.add(new Node("9.1","9","9.1"));

        list.add(new Node("1","0","1"));
        list.add(new Node("1.1","1","1.1"));

        list.add(new Node("2","0","2"));
        list.add(new Node("2.1","2","2.1"));

        list.add(new Node("3","0","3"));
        list.add(new Node("3.1","3","3.1"));
        list.add(new Node("3.1.2","3.1","3.1.2"));
        Node root = test.getRoot(list);
        Result<Object> result = new Result<>();
        System.out.println(root);
        result.setResult(root);
        return result;
    }
}
class Node{
    String id;
    String pid;
    String name;
    List<Node> children;
    public Node(String id,String pid,String name){
        this.id=id;
        this.pid = pid;
        this.name = name;
        this.children = new ArrayList<>();
    }

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }

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

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

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

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPid() {
        return pid;
    }
}

class Test{

    public Node getRoot(List<Node> list){
        Node root=null;
        Map<String,Node> map= new HashMap<String,Node>();
        for(Node n:list){
            String id = n.getId();
            Node node = map.get(id);
            if(node==null){
                node = n;
                map.put(id,n);
            }else{
                node = map.get(id);
                node.setPid(n.getPid());
                node.setName(n.getName());
            }
            String pid = node.getPid();
            if(pid.equals("-1")) { //根结点的判断
                root = node;
                continue;
            }
            Node node1 = map.get(pid);
            if(node1==null){
                node1 = new Node(pid,"-1","头节点");
                map.put(pid,node1);
            }
            node1.getChildren().add(node);
        }
        return  root;
    }
}

测试结果

代码语言:javascript
复制
{
  "success": true,
  "message": "操作成功!",
  "code": 0,
  "result": {
    "id": "0",
    "pid": "-1",
    "name": "根节点",
    "children": [
      {
        "id": "9",
        "pid": "0",
        "name": "9",
        "children": [
          {
            "id": "9.1",
            "pid": "9",
            "name": "9.1",
            "children": [
              
            ]
          }
        ]
      },
      {
        "id": "1",
        "pid": "0",
        "name": "1",
        "children": [
          {
            "id": "1.1",
            "pid": "1",
            "name": "1.1",
            "children": [
              
            ]
          }
        ]
      },
      {
        "id": "2",
        "pid": "0",
        "name": "2",
        "children": [
          {
            "id": "2.1",
            "pid": "2",
            "name": "2.1",
            "children": [
              
            ]
          }
        ]
      },
      {
        "id": "3",
        "pid": "0",
        "name": "3",
        "children": [
          {
            "id": "3.1",
            "pid": "3",
            "name": "3.1",
            "children": [
              {
                "id": "3.1.2",
                "pid": "3.1",
                "name": "3.1.2",
                "children": [
                  
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "timestamp": 1651161878529
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-04-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档