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

 更新时间:2020年01月15日 10:07:51   作者:炫舞风中  
这篇文章主要介绍了Java数据封装树形结构代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了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": []
  }
 ]
}

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

相关文章

  • Java使用substring()截取(提取)子字符串

    Java使用substring()截取(提取)子字符串

    这篇文章主要介绍了Java使用substring()截取(提取)子字符串,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 关于SpringCloud的Bus消息总线图文详解

    关于SpringCloud的Bus消息总线图文详解

    这篇文章主要介绍了关于SpringCloud的Bus消息总线图文详解,Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能,需要的朋友可以参考下
    2023-05-05
  • @OneToMany查询陷入循环引用的解决方案

    @OneToMany查询陷入循环引用的解决方案

    这篇文章主要介绍了@OneToMany查询陷入循环引用的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Springboot在IDEA热部署的配置方法

    Springboot在IDEA热部署的配置方法

    这篇文章主要介绍了Springboot在IDEA热部署的配置方法,给大家补充介绍了Intellij IDEA 4种配置热部署的方法,需要的朋友可以参考下
    2018-04-04
  • java ExecutorService CompletionService线程池区别与选择

    java ExecutorService CompletionService线程池区别与选择

    这篇文章主要为大家介绍了java ExecutorService CompletionService线程池区别与选择使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • Java OpenCV实现人脸识别过程详解

    Java OpenCV实现人脸识别过程详解

    这篇文章主要介绍了Java OpenCV实现人脸识别过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Java中String判断值为null或空及地址是否相等的问题

    Java中String判断值为null或空及地址是否相等的问题

    这篇文章主要介绍了Java中String判断值为null或空及地址是否相等的问题,文中举了简单的例子对字符串类型的值和地址问题进行讲解,需要的朋友可以参考下
    2016-01-01
  • Java 内置Http Server构建web应用案例详解

    Java 内置Http Server构建web应用案例详解

    这篇文章主要介绍了Java 内置Http Server构建web应用案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • java数据结构之树基本概念解析及代码示例

    java数据结构之树基本概念解析及代码示例

    这篇文章主要介绍了java数据结构之树基本概念解析及代码示例,介绍了树的定义,基本术语,主要操作及实现等相关内容,具有一定参考价值,需要的朋友可了解下。
    2017-11-11
  • java把字符串写入文件里的简单方法分享

    java把字符串写入文件里的简单方法分享

    这篇文章主要介绍了java把字符串写入到文件里的简单方法,这是跟一个外国朋友学的代码,需要的朋友可以参考下
    2014-03-03

最新评论