zoukankan      html  css  js  c++  java
  • Java 组装 Tree

    import java.util.ArrayList;
    import java.util.List;
    
    
    public class TreeNode {
        protected Long id;
        protected Long parentId;
    
        public List<TreeNode> getChildren() {
            return children;
        }
    
        public void setChildren(List<TreeNode> children) {
            this.children = children;
        }
    
          List<TreeNode> children = new ArrayList<TreeNode>(10);
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public Long getParentId() {
            return parentId;
        }
    
        public void setParentId(Long parentId) {
            this.parentId = parentId;
        }
    
        public void add(TreeNode node){
            children.add(node);
        }
    }
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class TreeUtil {
      /**
       * 两层循环实现建树
       *
       * @param treeNodes 传入的树节点列表
       * @return
       */
      public static <T extends TreeNode> List<T> bulid(List<T> treeNodes, Object root) {
    
        List<T> trees = new ArrayList<T>(10);
    
        for (T treeNode : treeNodes) {
    
          if (root.equals(treeNode.getParentId())) {
            trees.add(treeNode);
          }
    
          for (T it : treeNodes) {
            if (it.getParentId().equals(treeNode.getId())) {
              if (treeNode.getChildren() == null) {
                treeNode.setChildren(new ArrayList<TreeNode>(10));
              }
              treeNode.add(it);
            }
          }
        }
        return trees;
      }
    
      /**
       * 使用递归方法建树
       *
       * @param treeNodes
       * @return
       */
      public static <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes,Object root) {
        List<T> trees = new ArrayList<T>(10);
        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().equals(it.getParentId()) ) {
            if (treeNode.getChildren() == null) {
              treeNode.setChildren(new ArrayList<TreeNode>(10));
            }
            treeNode.add(findChildren(it, treeNodes));
          }
        }
        return treeNode;
      }
    
    
    
    
    }
  • 相关阅读:
    Android系统源代码下载
    Windows Embedded Compact 7初体验
    windowsmobile 开发环境
    Windows X64汇编入门(1)
    汇编语言的Hello World
    如何构建Win32汇编的编程环境(ONEPROBLEM个人推荐)
    音频视频解决方案:GStreamer/ffmpeg/ffdshow/directshow/vfw
    汇编开发环境
    DirectX
    关于DirectShow SDK 和Windows SDK,及DirectX SDK
  • 原文地址:https://www.cnblogs.com/mingforyou/p/14677456.html
Copyright © 2011-2022 走看看