Java利用TreeUtils工具类实现列表转树
作者:赛先生和泰先生 时间:2021-10-02 03:28:00
一、序言
在日常一线开发过程中,总有列表转树的需求,几乎是项目的标配,比方说做多级菜单、多级目录、多级分类等,有没有一种通用且跨项目的解决方式呢?帮助广大技术朋友给业务瘦身,提高开发效率。
本文将基于Java8的Lambda 表达式和Stream等知识,使用TreeUtils工具类实现一行代码完成列表转树这一通用型需求。本文有配套视频,传送门直达。
需要说明的是,本TreeUtils工具类已进行三版优化,能够胜任生产场景绝大多数列表转树的。
二、实战编码
1、引入坐标
本坐标地址包含TreeUtils
工具类方法,已经发布到Maven中央仓库,建议使用最新的版本号,本文发布时版本是1.5.8.3
,若有最新版本,建议使用最新版本。
学习源码的朋友,源码直通车
<dependency>
<groupId>xin.altitude.cms</groupId>
<artifactId>ucode-cms-common</artifactId>
<version>1.5.8.3</version>
</dependency>
2、编写DO
DO是访问数据库实体类,属于前期准备素材。
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "bu_category3")
public class Category {
private static final long serialVersionUID = 1L;
@TableId(type = IdType.AUTO)
private String id;
private String name;
private String parentId;
private String remark;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
public Category(Category category) {
if (Objects.nonNull(category)) {
this.id = category.id;
this.name = category.name;
this.parentId = category.parentId;
this.remark = category.remark;
this.createTime = category.createTime;
this.updateTime = category.updateTime;
}
}
}
3、创建BO
创建BO数据模型用于构造属性结点
@Data
@NoArgsConstructor
public class CategoryBo {
private String id;
private String name;
private String parentId;
private String remark;
/**
* 孩子结点 名字任意取
*/
private List<CategoryBo> childList;
/**
* 实现Category 转 CategoryBo
*
* @param category
*/
public CategoryBo(Category category) {
if (Objects.nonNull(category)) {
this.id = category.getId();
this.name = category.getName();
this.parentId = category.getParentId();
this.remark = category.getRemark();
}
}
}
3、调用TreeUtils工具类
在服务层调用TreeUtils工具类,一行代码完成转换。
/**
* 获取DO中指定的属性 并构造成树
* @return
*/
public List<CategoryBo> selectCategory4() {
List<Category> categoryList = this.list();
List<CategoryBo> categoryBoList = EntityUtils.toList(categoryList, CategoryBo::new);
return TreeUtils.createNodeDetail(categoryBoList, "000000", CategoryBo::getId, CategoryBo::getParentId, CategoryBo::getChildList);
}
4、效果展示
原始列表数据
{
"code": 200,
"msg": "操作成功",
"data": [
{
"id": "310000",
"name": "电子商务",
"parentId": "000000",
"remark": "EEEEE",
"createTime": null,
"updateTime": "2021-11-21 11:22:40"
},
{
"id": "310100",
"name": "大宗商品",
"parentId": "310000",
"remark": "JJJJJ",
"createTime": null,
"updateTime": "2021-11-21 11:22:52"
},
{
"id": "310101",
"name": "大宗商品综合",
"parentId": "310100",
"remark": "IIIII",
"createTime": null,
"updateTime": "2021-11-21 11:22:58"
},
{
"id": "310102",
"name": "钢铁类电商",
"parentId": "310100",
"remark": "OOOOO",
"createTime": null,
"updateTime": "2021-11-21 11:23:02"
},
{
"id": "310200",
"name": "综合电商",
"parentId": "310000",
"remark": "NNNNN",
"createTime": null,
"updateTime": "2021-11-21 11:23:07"
}
]
}
树状层次数据
{
"code": 200,
"msg": "操作成功",
"data": [
{
"id": "310000",
"name": "电子商务",
"parentId": "000000",
"remark": "EEEEE",
"childList": [
{
"id": "310100",
"name": "大宗商品",
"parentId": "310000",
"remark": "JJJJJ",
"childList": [
{
"id": "310101",
"name": "大宗商品综合",
"parentId": "310100",
"remark": "IIIII",
"childList": []
},
{
"id": "310102",
"name": "钢铁类电商",
"parentId": "310100",
"remark": "OOOOO",
"childList": []
}
]
},
{
"id": "310200",
"name": "综合电商",
"parentId": "310000",
"remark": "NNNNN",
"childList": []
}
]
}
]
}
三、小结
通过一个小小的高频业务场景,抽离业务共性,屏蔽业务细节,实现了通用型业务逻辑的编码。
希望广大技术朋友在完成日常开发工作的前提下,能够进一步打磨代码,感受编程之美。
来源:https://www.cnblogs.com/javazhishitupu/p/16914381.html
标签:Java,TreeUtils,列表,树
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
jsp如何获取Session中的值
2023-07-01 05:59:28
Java客户端调用.NET的WebService实例
2023-11-03 17:22:00
![](https://img.aspxhome.com/file/2023/8/58988_0s.png)
详解Java中的线程池
2023-11-10 16:33:27
![](https://img.aspxhome.com/file/2023/9/59369_0s.png)
Default Methods实例解析
2023-05-18 19:28:01
Springboot实现Shiro整合JWT的示例代码
2022-01-30 02:58:32
![](https://img.aspxhome.com/file/2023/1/65601_0s.png)
C#与C++ dll之间传递字符串string wchar_t* char* IntPtr问题
2022-09-16 09:34:53
SpringCloud Zuul基本使用方法汇总
2023-11-14 15:21:54
Java由浅入深讲解继承下
2023-11-25 12:21:26
![](https://img.aspxhome.com/file/2023/2/59742_0s.png)
SpringBoot利用jackson格式化时间的三种方法
2022-03-26 22:28:56
![](https://img.aspxhome.com/file/2023/1/71191_0s.png)
Java设计模式之工厂模式实现方法详解
2023-11-26 07:55:51
Java内存模型final的内存语义
2023-06-05 08:02:25
![](https://img.aspxhome.com/file/2023/0/62130_0s.png)
C#实现类似新浪微博长URL转短地址的方法
2023-06-02 12:59:28
Spring框架学习之Cache抽象详解
2023-07-20 17:37:47
详解Kotlin:forEach也能break和continue
2022-05-03 01:24:10
spring boot2结合mybatis增删改查的实现
2023-07-07 01:17:31
![](https://img.aspxhome.com/file/2023/4/61644_0s.png)
JavaWeb开发使用Cookie创建-获取-持久化、自动登录、购物记录、作用路径
2022-01-11 10:37:28
![](https://img.aspxhome.com/file/2023/2/64902_0s.png)
基于eclipse.ini内存设置的问题详解
2021-08-25 02:56:55
Struts2实现对action请求对象的拦截操作方法
2023-06-08 01:54:13
![](https://img.aspxhome.com/file/2023/2/70342_0s.jpg)
springboot实现定时任务的四种方式小结
2021-10-20 20:38:06
springboot整合kaptcha生成验证码功能
2023-07-14 21:48:51
![](https://img.aspxhome.com/file/2023/6/71896_0s.png)