标准建树工具类

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

 

上一篇:深入理解“同步-异步-阻塞-非阻塞”


下一篇:树形结构工具类,如:菜单、部门等