欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java数据封装树形结构代码实例

程序员文章站 2023-01-25 18:39:10
这篇文章主要介绍了java数据封装树形结构代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1、实体类 @data...

这篇文章主要介绍了java数据封装树形结构代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、实体类

@data
public class publishservicetype implements comparable<publishservicetype>{


  /**
   * 
   */
  private static final long serialversionuid = -3572108154932898825l;


  /* 
   * @see [code]
   * @comment 类型标识
   */
  private string code;
  /* 
   * @see {createtime}
   * @comment 创建时间
   */
  private java.util.date createtime;
  /* 
   * @see {defaultmanual}
   * @comment 服务类型默认使用手册
   */
  private string defaultmanual;
  /* 
   * @see {description}
   * @comment 服务类型描述
   */
  private string description;
  /* 
   * @see {id}
   * @comment 主键
   */
  private string id;
  /* 
   * @see {isdelete}
   * @comment 是否可以删除
   */
  private integer isdelete;
  /* 
   * @see {lastmodifytime}
   * @comment 最近修改时间
   */
  private java.util.date lastmodifytime;
  /* 
   * @see {name}
   * @comment 服务类型名称
   */
  private string name;
  /* 
   * @see {parentid}
   * @comment 服务类型父节点
   */
  private string parentid;

  /**
   * 排序
   */
  private integer sort;

  private list<publishservicetype>children;
}

2、数据封装

@override
  public list<publishservicetype> findlist(string name) {
    list<publishservicetype>list = publishservicetypemapper.findbyname(name);
    if (judgeutil.isempty(list)){
      return null;
    }
    //父子级组装
    return parentandchildren(list);
  }
 private list<publishservicetype>parentandchildren(list<publishservicetype> list){

    //最顶层根节点
    list<publishservicetype>rootlist = new arraylist<>();
    //非最顶层根节点
    list<publishservicetype>bodylist = new arraylist<>();
    for (publishservicetype publishservicetype : list) {
      if (stringutils.isblank(publishservicetype.getparentid())){
        rootlist.add(publishservicetype);
      }else{
        bodylist.add(publishservicetype);
      }
    }
    return gettree(rootlist,bodylist);
  }

  public list<publishservicetype> gettree(list<publishservicetype>rootlist, list<publishservicetype>bodylist){
    if (!judgeutil.isempty(bodylist)){
      //声明一个map,用来过滤已操作过的数据
      map<string,string> map = new hashmap<>(bodylist.size());
      rootlist.foreach(parent->getchild(parent,bodylist,map));
      return rootlist;
    }else{
      return rootlist;
    }
  }

  private void getchild(publishservicetype parent,list<publishservicetype>bodylist, map<string,string> map){
    list<publishservicetype>childlist = new arraylist<>();
    bodylist.stream().filter(c->!map.containskey(c.getid()))
             .filter(c->c.getparentid().equals(parent.getid()))
             .foreach(c->{
               map.put(c.getid(),c.getparentid());
               getchild(c,bodylist,map);
               childlist.add(c);
             });
    
    parent.setchildren(childlist);
  }

3、结果

{
 "code": 20000,
 "message": "成功",
 "data": [
  {
   "code": null,
   "createtime": null,
   "defaultmanual": null,
   "description": null,
   "id": "dc1d70b9eb7b4df3bbe8dcc6a93cbd57",
   "isdelete": -1,
   "lastmodifytime": null,
   "name": "基础服务",
   "parentid": "",
   "sort": 1,
   "children": [
    {
     "code": null,
     "createtime": null,
     "defaultmanual": null,
     "description": null,
     "id": "b1779671ef1b45e0a9a8a1edbff03f1e",
     "isdelete": -1,
     "lastmodifytime": null,
     "name": "数据源服务",
     "parentid": "dc1d70b9eb7b4df3bbe8dcc6a93cbd57",
     "sort": 2,
     "children": [
      {
       "code": null,
       "createtime": null,
       "defaultmanual": null,
       "description": null,
       "id": "2a38a8254ec348e9b54c9bf4622f23db",
       "isdelete": 1,
       "lastmodifytime": null,
       "name": "测试添加数据库服务2",
       "parentid": "b1779671ef1b45e0a9a8a1edbff03f1e",
       "sort": null,
       "children": []
      }
     ]
    },
    {
     "code": null,
     "createtime": null,
     "defaultmanual": null,
     "description": null,
     "id": "d4f3b047dc2d467a9b404ded8acf4673",
     "isdelete": 1,
     "lastmodifytime": null,
     "name": "text_lsa",
     "parentid": "dc1d70b9eb7b4df3bbe8dcc6a93cbd57",
     "sort": null,
     "children": []
    }
   ]
  },
  {
   "code": null,
   "createtime": null,
   "defaultmanual": null,
   "description": null,
   "id": "af1b4a4d2f074fa19e1dae0a5540a5bf",
   "isdelete": 1,
   "lastmodifytime": null,
   "name": "测试添加1",
   "parentid": "",
   "sort": null,
   "children": []
  },
  {
   "code": null,
   "createtime": null,
   "defaultmanual": null,
   "description": null,
   "id": "62e15d859a224126884888a55df355a7",
   "isdelete": 1,
   "lastmodifytime": null,
   "name": "测试添加2",
   "parentid": "",
   "sort": null,
   "children": []
  }
 ]
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。