参考地址: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数据封装成树形结构,多级的更多相关文章

  1. c# List列表数据转换成树形结构

    把List列表结构 转换成树形结构 /// <summary> /// 构造树形Json /// </summary> public static class TreeJson ...

  2. 使用js将后台返回的数据转换成树形结构

    将类似如下数据转换成树形的数据: [ { id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1 ...

  3. 记一则 Lambda内递归调用方法将集合对象转换成树形结构

    public dynamic GetDepartments(string labID) { List<int> usedIDs = new List<int>(); //缓存已 ...

  4. javascript将平行的拥有上下级关系的数据转换成树形结构

    转换函数 var Littlehow = {}; /** * littlehow 2019-05-15 * 平行数据树形转换器 * @type {{format: tree.format, sort: ...

  5. 《Java数据结构》树形结构

    树形结构是一层次的嵌套结构. 一个树形结构的外层和内层有相似的结构, 所以这种结构多可以递归的表示.经典数据结构中的各种树形图是一种典型的树形结构:一颗树可以简单的表示为根, 左子树, 右子树. 左子 ...

  6. java list实现树形结构

    1.javabean import java.util.List; public class TreeNode { private String id; private String parentId ...

  7. Word排版成树形结构技巧

    初始文字 A A1 A2 B1 B1 B2 C C1 希望效果 关健设置

  8. dom4j 解析字符串成树形结构

    引入maven依赖: <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artif ...

  9. idea目录结构子目录在父目录后面跟着改成树形结构

    1.点击项目窗口的设置按钮 2.取消Compact Middle Packages选项的对勾即可

随机推荐

  1. Promethus

    https://blog.csdn.net/zl1zl2zl3/article/details/74332437

  2. MySQL技术内幕 InnoDB存储引擎 之 InnoDB体系架构

    后台线程 1.Master Thread 2.IO Thread 3.Purge Thread 4.Page Cleaner Thread  内存 重做日志在以下三种情况下将重做日志缓存中的内容刷新到 ...

  3. maven 成长之路

    1配置maven 环境变量 新建系统变量 M2_HOME :E:\apache-maven-3.5.2 在系统变量 path中添加 E:\apache-maven-3.5.2\bin 运行 mvn - ...

  4. 在SOUI3.0中使用数值动画

    上一篇介绍了插值动画,插值动画是直接作用于窗口对象的. 数值动画则可以作用于任何对象. SOUI内置了3种数值类型的动画,分别是SIntAnimator, SFloatAnimator, SColor ...

  5. Flink组件及特性

    Flink 是一个针对流数据和批数据的分布式处理引擎.它主要是由 Java 代码实现.目前主要还是依靠开源社区的贡献而发展.对 Flink 而言,其所要处理的主要场景就是流数据,批数据只是流数据的一个 ...

  6. wcf restful 访问报错 *.svc HTTP error 404.17 - Not Found

    安装完成 iisreset,即使不重启也已经可以使用了

  7. 【MM系列】MB1A MB1B MB1C MB11 MIGO的区别解析

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]MB1A MB1B MB1C MB1 ...

  8. cocos2dx基础篇(8) 开关按钮CCControlSwitch

    [3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)标签类 LabelTTF 改为 Label (4)CCControlEvent 改为强枚举 Control::Ev ...

  9. Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.4 Predicates and Quantifiers

    The statements that describe valid input are known as preconditions and the conditions that the outp ...

  10. 【Qt开发】Qt在Windows下的三种编程环境搭建

    从QT官网可以得知其支持的平台.编译器和调试器的信息如图所示: http://qt-project.org/doc/qtcreator-3.0/creator-debugger-engines.htm ...