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

本文介绍了一种在Java中实现树形结构数据封装的方法。通过定义实体类PublishServiceType并利用其属性,如id、parentId等,实现了树形结构的数据组装。文章提供了完整的代码示例,包括实体类定义及数据封装逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1、实体类

@data

public class publishservicetype implements comparable{

/**

*

*/

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 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": []

}

]

}

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

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值