Java构建树结构的公共方法

打印 上一主题 下一主题

主题 945|帖子 945|积分 2835

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一、前提


  • pId需要传入用来确认第一级的父节点,而且pId可以为null。
  • 树实体类必须实现:TreeNode接口
  • MyTreeVo必须有这三个属性:id、pId、children
  • 可以根据不同需求,配置TreeNode和MyTreeVo中固定的属性
二、代码


  • 定义TreeNode接口
  1. public interface TreeNode {
  2.     String getId();
  3.     String getpId();
  4.     List getChildren();
  5. }
复制代码

  • 需要将pId作为参数传入,在方法中添加一个pId参数,用于确认第一级的父节点。
    -- 这个示例代码中,buildTree方法接收两个参数,一个是泛型类型的参数List,另一个是pId,用于确认第一级的父节点。在找到所有的根节点后,我们需要对每个根节点调用buildChildren方法,递归构建子节点。
    -- 以便支持传入的pId作为顶级节点
  1. public class TreeBuilder {
  2.     /**
  3.      * @param nodes 需要构建的nodes,不要继承 TreeNode,且需要有 TreeNode 下的三个方法
  4.      * @param pId 父id。为null则是所有pid为null的是顶级节点;不为null,则是从给的pId往下查询并构建树
  5.      **/
  6.     public static <T extends TreeNode> List<T> buildTree(List<T> nodes, String pId) {
  7.         List<T> roots = new ArrayList<>();
  8.         for (T node : nodes) {
  9.             if (node.getId().equals(pId) || (node.getParentId() == null && (pId == null || pId.equals(node.getId())))) {
  10.                 roots.add(node);
  11.             }
  12.         }
  13.         for (T root : roots) {
  14.             buildChildren(root, nodes);
  15.         }
  16.         return roots;
  17.     }
  18.     private static <T extends TreeNode> void buildChildren(T node, List<T> nodes) {
  19.         for (T child : nodes) {
  20.             if (child.getParentId() != null && child.getParentId().equals(node.getId())) {
  21.                 node.getChildren().add(child);
  22.                 buildChildren(child, nodes);
  23.             }
  24.         }
  25.     }
  26. }
复制代码

  • 构建到几级
    -- 如果再增加一个参数,构建到树的第几层该如何实现?
    -- 根据需求,如果增加一个参数来控制构建到树的第几层,可以在buildChildren方法中添加一个level参数,用于记录当前节点的层数。在递归构建子节点时,如果当前节点的层数小于指定的层数,则继续递归构建子节点,否则停止递归。以下是修改后的Java示例代码:
  1. public class TreeBuilder {
  2.      
  3.      /**
  4.       * @param level 级别 从1开始。1是最顶层
  5.       **/
  6.     public static <T extends TreeNode> List<T> buildTree(List<T> nodes, String pId, int level) {
  7.         List<T> roots = new ArrayList<>();
  8.         for (T node : nodes) {
  9.             if (node.getId().equals(pId) || (node.getParentId() == null && (pId == null || pId.equals(node.getId())))) {
  10.                 roots.add(node);
  11.             }
  12.         }
  13.         for (T root : roots) {
  14.             buildChildren(root, nodes, level, 1);
  15.         }
  16.         return roots;
  17.     }
  18.     private static <T extends TreeNode> void buildChildren(T node, List<T> nodes, int level, int currentLevel) {
  19.         if (currentLevel >= level) {
  20.             return;
  21.         }
  22.         for (T child : nodes) {
  23.             if (child.getParentId() != null && child.getParentId().equals(node.getId())) {
  24.                 if (node.getChildren() == null) {
  25.                     node.setChildren(new ArrayList<>());
  26.                 }
  27.                 node.getChildren().add(child);
  28.                 buildChildren(child, nodes, level, currentLevel + 1);
  29.             }
  30.         }
  31.     }
  32. }
复制代码
三、使用

1、实现TreeNode接口
  1. public class MyTreeVo implements TreeNode {
  2.     /**
  3.      * 主键
  4.      */
  5.     private String id;
  6.     /**
  7.      * 父节点ID
  8.      */
  9.     private String pId;
  10.     /**
  11.      * 子级
  12.      */
  13.     private List<MyTreeVo> children = Lists.newArrayList();
  14.     //其他属性……
  15.     public List<MyTreeVo> getChildren() {
  16.         return children;
  17.     }
  18.     public String getId() {
  19.         return id;
  20.     }
  21.     public String getpId() {
  22.         return pId;
  23.     }
  24.     //其他属性的getter、setter……
复制代码
2、使用

-- pId可以传入null,也可以传入需要从哪个节点(X)开始构造的 X的id
-- pId比如可以传入3、样例中的 “二、噢噢噢噢”的id=“e6ee51485389495cb923a122be800012”。然后构建出来的,就是“二、噢噢噢噢”的下级树
  1. List<MyTreeVo> tree = TreeUtilQz.buildTree(vos,null);
  2. //tree就是构建好的树结构数据
复制代码
3、样例
  1. {
  2.     "data": [
  3.         {
  4.             "id": "e6ee51485389495cb923a122be800011",
  5.             "pId": "",
  6.             "name": "一、钢管钢管",
  7.             "children": [
  8.                 {
  9.                     "id": "e6ee51485389495cb923a122be800014",
  10.                     "pId": "e6ee51485389495cb923a122be800011",
  11.                     "name": "(二)嘎嘎嘎嘎嘎",
  12.                     "children": []
  13.                 },
  14.                 {
  15.                     "id": "e6ee51485389495cb923a122be800013",
  16.                     "pId": "e6ee51485389495cb923a122be800011",
  17.                     "name": "(一)顶顶顶顶",
  18.                     "children": []
  19.                 }
  20.             ]
  21.         },
  22.         {
  23.             "id": "e6ee51485389495cb923a122be800012",
  24.             "pId": "",
  25.             "name": "二、噢噢噢噢",
  26.             "children": [
  27.                 {
  28.                     "id": "e6ee51485389495cb923a122be800015",
  29.                     "pId": "e6ee51485389495cb923a122be800012",
  30.                     "name": "二的下级",
  31.                     "children": [
  32.                         {
  33.                             "id": "e6ee51485389495cb923a122be800016",
  34.                             "pId": "e6ee51485389495cb923a122be800015",
  35.                             "name": "二的下级的下级",
  36.                             "children": []
  37.                         }
  38.                     ]
  39.                 }
  40.             ]
  41.         }
  42.     ]
  43. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表