标准建树工具类

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;
    }
}

 

posted @ 2021-11-23 18:23  二次元的程序猿  阅读(36)  评论(0编辑  收藏  举报