• 标准建树工具类


    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;
        }
    }
    随笔看心情
  • 相关阅读:
    js数组
    js字符串和控制语句
    生成器、列表解析
    js属性
    js函数
    js变量
    python抽象方法
    python 装饰器的缺点以及解决方法
    windows文件关联、打开方式列表之修改注册表攻略
    JavaScript定时器及其他
  • 原文地址:https://www.cnblogs.com/stromgao/p/15594559.html
Copyright © 2020-2023  润新知