其他分享
首页 > 其他分享> > 标准建树工具类

标准建树工具类

作者:互联网

public class RecursiveUtil {
    public RecursiveUtil() {
    }

    public static <T extends PModel<T>> List<T> buildRecursive(List<T> treeNodes) {
        List<T> trees = new ArrayList();
        Iterator var2 = treeNodes.iterator();

        while(var2.hasNext()) {
            T treeNode = (PModel)var2.next();
            if (0L == treeNode.getPid()) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }

        return trees;
    }

    private static <T extends PModel<T>> T findChildren(T treeNode, List<T> treeNodes) {
        treeNode.setChildren(new ArrayList());
        Iterator var2 = treeNodes.iterator();

        while(var2.hasNext()) {
            T it = (PModel)var2.next();
            if (treeNode.getId().equals(it.getPid())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList());
                }

                treeNode.getChildren().add(findChildren(it, treeNodes));
            }
        }

        return treeNode;
    }

    public static <T extends ParentModel<T>> List<T> buildRecursiveP(List<T> treeNodes) {
        List<T> trees = new ArrayList();
        Iterator var2 = treeNodes.iterator();

        while(var2.hasNext()) {
            T treeNode = (ParentModel)var2.next();
            if (0L == treeNode.getParent()) {
                trees.add(findChildrenP(treeNode, treeNodes));
            }
        }

        return trees;
    }

    private static <T extends ParentModel<T>> T findChildrenP(T treeNode, List<T> treeNodes) {
        treeNode.setChildren(new ArrayList());
        Iterator var2 = treeNodes.iterator();

        while(var2.hasNext()) {
            T it = (ParentModel)var2.next();
            if (treeNode.getId().equals(it.getParent())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList());
                }

                treeNode.getChildren().add(findChildrenP(it, treeNodes));
            }
        }

        return treeNode;
    }
}

 

标签:treeNode,var2,List,trees,标准,new,工具,建树,treeNodes
来源: https://www.cnblogs.com/stromgao/p/15594559.html