栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

1、实体类

@data
public class PublishServiceType implements Comparable{


  
  private static final long serialVersionUID = -3572108154932898825L;


  
  private String code;
  
  private java.util.Date createtime;
  
  private String defaultmanual;
  
  private String description;
  
  private String id;
  
  private Integer isdelete;
  
  private java.util.Date lastmodifytime;
  
  private String name;
  
  private String parentid;

  
  private Integer sort;

  private Listchildren;
}

2、数据封装

@Override
  public List findList(String name) {
    Listlist = publishServiceTypeMapper.findByName(name);
    if (JudgeUtil.isEmpty(list)){
      return null;
    }
    //父子级组装
    return parentAndChildren(list);
  }
 private ListparentAndChildren(List list){

    //最顶层根节点
    ListrootList = new ArrayList<>();
    //非最顶层根节点
    ListbodyList = new ArrayList<>();
    for (PublishServiceType publishServiceType : list) {
      if (StringUtils.isBlank(publishServiceType.getParentid())){
 rootList.add(publishServiceType);
      }else{
 bodyList.add(publishServiceType);
      }
    }
    return getTree(rootList,bodyList);
  }

  public List getTree(ListrootList, ListbodyList){
    if (!JudgeUtil.isEmpty(bodyList)){
      //声明一个map,用来过滤已操作过的数据
      Map map = new HashMap<>(bodyList.size());
      rootList.forEach(parent->getChild(parent,bodyList,map));
      return rootList;
    }else{
      return rootList;
    }
  }

  private void getChild(PublishServiceType parent,ListbodyList, Map map){
    ListchildList = 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": []
  }
 ]
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135462.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号