天天看點

List轉換為tree-項目真實使用

前提:資料庫中查出來每一條資料構成連結清單,需要我們轉換成樹結構,id,pid(父節點的id) name

主要代碼:public Node getRoot(List<Node> list)

0為根節點 1-9為次根節點 1.1-1.9 2.1-2.9 … 為次次根節點 看下方測試結果注意隻有0的pid為-1

@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;
    }
}      
{
  "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
}      
  1. 每次先檢視map中是不是有id對應的key,如果有的話,說明之前添加過但是添加的對象隻有id和children,這時候就需要把對應value中的對象的其他變量指派,這個為什麼隻有id一會看到下邊就知道了。

    如果沒有對應的key是id的話,添加key為id ,value為Node對象。

  2. 1中添加完id對應的Node之後,如果目前id節點的pid節點已存在,則把目前id節點添加到pid節點children數組中。如果不存在,需要先建立一個pid的新節點,并把新節點的id=pid,新節點添加到map中,并把1中的Node節點添加到新結點的children中(知道1中那個為什麼隻有id和children了吧)。