一些代碼

package com.slm.main;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

public class Application {


    public static void main(String[] args) {
        TreeNode re = re(0);
        System.out.println(JSON.toJSONString(re));
    }

    /**
     * 根節點--根節點
     *          |----節點
     *                |---子孩子
     *          |----節點
     *          |----節點
     *          |----節點
     * @param id
     * @return
     */
    public static TreeNode re(int id) {
        Org byId = DB.getById(id);
        TreeNode treeNode = TreeNode.fromOrg(byId);
        List<Org> byParentId = getByParentId(byId.getId());
        treeNode.setChildren(new ArrayList<>());
        for (Org org : byParentId) {
            TreeNode re = re(org.getId());
            treeNode.getChildren().add(re);
        }
        return treeNode;
    }

    public static List<Org> getByParentId(int parentId) {
        List<Org> ret = new ArrayList<>();
        for (Org org : DB.list) {
            if (org.getParentId().equals(parentId)) {
                ret.add(org);
            }
        }
        return ret;
    }
}
相關文章
相關標籤/搜索