Java数据封装成树形结构,多级
参考地址:https://blog.csdn.net/chendu500qiang/article/details/91493147
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数据封装成树形结构,多级的更多相关文章
- c# List列表数据转换成树形结构
把List列表结构 转换成树形结构 /// <summary> /// 构造树形Json /// </summary> public static class TreeJson ...
- 使用js将后台返回的数据转换成树形结构
将类似如下数据转换成树形的数据: [ { id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1 ...
- 记一则 Lambda内递归调用方法将集合对象转换成树形结构
public dynamic GetDepartments(string labID) { List<int> usedIDs = new List<int>(); //缓存已 ...
- javascript将平行的拥有上下级关系的数据转换成树形结构
转换函数 var Littlehow = {}; /** * littlehow 2019-05-15 * 平行数据树形转换器 * @type {{format: tree.format, sort: ...
- 《Java数据结构》树形结构
树形结构是一层次的嵌套结构. 一个树形结构的外层和内层有相似的结构, 所以这种结构多可以递归的表示.经典数据结构中的各种树形图是一种典型的树形结构:一颗树可以简单的表示为根, 左子树, 右子树. 左子 ...
- java list实现树形结构
1.javabean import java.util.List; public class TreeNode { private String id; private String parentId ...
- Word排版成树形结构技巧
初始文字 A A1 A2 B1 B1 B2 C C1 希望效果 关健设置
- dom4j 解析字符串成树形结构
引入maven依赖: <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artif ...
- idea目录结构子目录在父目录后面跟着改成树形结构
1.点击项目窗口的设置按钮 2.取消Compact Middle Packages选项的对勾即可
随机推荐
- leetcode-mid-Linked list- 200. Number of Islands¶
mycode 57.92% class Solution(object): def numIslands(self, grid): """ :type grid: Li ...
- C#中winform下利用ArcEngine调用ArcGIS Server发布的服务 AE9.3
主要使用了AE中的IAGSServerOject接口及IMapServer接口.Private void GetServerTest_Click(object sender, EventArgs e) ...
- Spring 之 IOC ,DI 理论
本文是依照极客学院java<Spring之IOC>章节学习的心得.随笔记录 浅谈IOC:(Inversion of Control, 控制反转) Spring 核心容器,贯穿始终.所谓IO ...
- 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第1节 基本概念_03maven一键构建概念
资料里面写好的Helloworld项目 pom.xml存放jar包的坐标 首先复制项目所在的目录的路径: 先进去复制的这个路径,然后又输入了d盘.这时候就进去到这个项目目录了. 这个项目就运行起来了. ...
- .NET中使用EF6与连接MYSQL
ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,不仅支持SQL Server,还支持MySQL.Ora ...
- Unity3D-Rigidbody
挂载Rigidbody的Gameobject受物理引擎的作用,有真实的物理力学. Mass质量:物体的质量(任意单位).建议一个物体的质量不要多余或少于其他单位的100倍. Drag阻力:当受力移动时 ...
- kubeadm搭建K8s集群及Pod初体验
基于Kubeadm 搭建K8s集群: 通过上一篇博客,我们已经基本了解了 k8s 的基本概念,也许你现在还是有些模糊,说真的我也是很模糊的.只有不断地操作去熟练,强化自己对他的认知,才能提升境界. 我 ...
- servlet3.0文件上传与下载
描述:文件上传与下载是在JavaEE中常见的功能,实现文件上传与下载的方式有多种,其中文件上传的方式有: (1)commons-fileupload: (2)Servlet 3.0 实现文件上传 (3 ...
- (二)inlineCallbacks,同步方式写异步代码
一. 上篇提到了用defered对象注册回调的方式,来处理异步操作,这样大家都知道,实际情况代码很难搞的.因为当业务逻辑复杂后,这边一个异步操作,我们注册一个回调,代码跳到A地方,A里面也有异步操作, ...
- 4-2如何判断字符串a是否以字符串b开头或结尾
1.相关介绍 1.1修改文件权限和查看文件权限 在windows平台实验时 os.chmod()无法将文件权限修改为可执行,暂不深究如何实现.在linux平台进行测试. (1)创建三个文件 pytho ...