美文网首页
java递归生成目录树

java递归生成目录树

作者: b_eethoven | 来源:发表于2020-09-30 17:43 被阅读0次

一、创建实体类

public class TreeNode {
    protected int id;
    protected int parentId;
    protected int level;
    protected String name;
    protected String code;
    protected String type;
    protected List<TreeNode> children = new ArrayList<>();

    public TreeNode(int id) {
        this.id = id;
    }

    public TreeNode(int id, int parentId) {
        this.id = id;
        this.parentId = parentId;
    }

    public TreeNode(int id, int parentId, String name) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
    }

    public TreeNode(int id, int parentId, String name, String code) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
        this.code = code;
    }

    public TreeNode(int id, int parentId, String name, int level) {
        this(id, parentId, name);
        this.level = level;
    }

    public TreeNode(int id, int parentId, String name, int level, String code) {
        this(id, parentId, name, level);
        this.code = code;
    }

    public TreeNode(int id, int parentId, String name, String code, String type) {
        this(id, parentId, name);
        this.code = code;
        this.type = type;
    }

    public void add(TreeNode node) {
        children.add(node);
    }
}

二、使用递归方法建树

  /**
   * 使用递归方法建树
   * @param treeNodes
   * @return
   */
  public static <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes,Object root) {
    List<T> trees = new ArrayList<T>();
    for (T treeNode : treeNodes) {
      if (root.equals(treeNode.getParentId())) {
        trees.add(findChildren(treeNode, treeNodes));
      }
    }
    return trees;
  }

  /**
   * 递归查找子节点
   * @param treeNodes
   * @return
   */
  public static <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) {
    for (T it : treeNodes) {
      if (treeNode.getId() == it.getParentId()) {
        if (treeNode.getChildren() == null) {
          treeNode.setChildren(new ArrayList<TreeNode>());
        }
        treeNode.add(findChildren(it, treeNodes));
      }
    }
    return treeNode;
  }

三、验证

public static void main(String[] args) {
    List<TreeNode> treeNodes = new ArrayList<>();
    TreeNode treeNode1 = new TreeNode(1,0,"一级目录1","一级目录1","一级目录1");
    TreeNode treeNode2 = new TreeNode(2,0,"一级目录2","一级目录2","一级目录2");

    TreeNode treeNode3 = new TreeNode(3,1,"二级目录1","二级目录1","二级目录1");
    TreeNode treeNode4 = new TreeNode(4,3,"三级目录1","三级目录1","三级目录1");

    TreeNode treeNode5 = new TreeNode(5,2,"二级目录2","二级目录2","二级目录2");
    TreeNode treeNode6 = new TreeNode(6,5,"三级目录2","三级目录2","三级目录2");

    treeNodes.add(treeNode1);
    treeNodes.add(treeNode2);
    treeNodes.add(treeNode3);
    treeNodes.add(treeNode4);
    treeNodes.add(treeNode5);
    treeNodes.add(treeNode6);
    List<TreeNode> treeNodes1 = buildByRecursive(treeNodes, 0);
    for (TreeNode treeNode : treeNodes1) {
      System.out.println("treeNode = " + treeNode);
    }

  }

四、结果

treeNode = TreeNode(id=1, parentId=0, level=0, name=一级目录1, code=一级目录1, type=一级目录1, children=[TreeNode(id=3, parentId=1, level=0, name=二级目录1, code=二级目录1, type=二级目录1, children=[TreeNode(id=4, parentId=3, level=0, name=三级目录1, code=三级目录1, type=三级目录1, children=[])])])
treeNode = TreeNode(id=2, parentId=0, level=0, name=一级目录2, code=一级目录2, type=一级目录2, children=[TreeNode(id=5, parentId=2, level=0, name=二级目录2, code=二级目录2, type=二级目录2, children=[TreeNode(id=6, parentId=5, level=0, name=三级目录2, code=三级目录2, type=三级目录2, children=[])])])

源码
https://306t.com/file/28095605-463131539
提取码:123456

相关文章

网友评论

      本文标题:java递归生成目录树

      本文链接:https://www.haomeiwen.com/subject/qfcpuktx.html