Java數(shù)據(jù)封裝樹(shù)形結(jié)構(gòu)代碼實(shí)例
這篇文章主要介紹了Java數(shù)據(jù)封裝樹(shù)形結(jié)構(gòu)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1、實(shí)體類
@data public class PublishServiceType implements Comparable<PublishServiceType>{ /** * */ private static final long serialVersionUID = -3572108154932898825L; /* * @see [code] * @comment 類型標(biāo)識(shí) */ private String code; /* * @see {createtime} * @comment 創(chuàng)建時(shí)間 */ private java.util.Date createtime; /* * @see {defaultmanual} * @comment 服務(wù)類型默認(rèn)使用手冊(cè) */ private String defaultmanual; /* * @see {description} * @comment 服務(wù)類型描述 */ private String description; /* * @see {id} * @comment 主鍵 */ private String id; /* * @see {isdelete} * @comment 是否可以刪除 */ private Integer isdelete; /* * @see {lastmodifytime} * @comment 最近修改時(shí)間 */ private java.util.Date lastmodifytime; /* * @see {name} * @comment 服務(wù)類型名稱 */ private String name; /* * @see {parentid} * @comment 服務(wù)類型父節(jié)點(diǎn) */ private String parentid; /** * 排序 */ private Integer sort; private List<PublishServiceType>children; }
2、數(shù)據(jù)封裝
@Override public List<PublishServiceType> findList(String name) { List<PublishServiceType>list = publishServiceTypeMapper.findByName(name); if (JudgeUtil.isEmpty(list)){ return null; } //父子級(jí)組裝 return parentAndChildren(list); } private List<PublishServiceType>parentAndChildren(List<PublishServiceType> list){ //最頂層根節(jié)點(diǎn) List<PublishServiceType>rootList = new ArrayList<>(); //非最頂層根節(jié)點(diǎn) 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)){ //聲明一個(gè)map,用來(lái)過(guò)濾已操作過(guò)的數(shù)據(jù) 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、結(jié)果
{ "code": 20000, "message": "成功", "data": [ { "code": null, "createtime": null, "defaultmanual": null, "description": null, "id": "dc1d70b9eb7b4df3bbe8dcc6a93cbd57", "isdelete": -1, "lastmodifytime": null, "name": "基礎(chǔ)服務(wù)", "parentid": "", "sort": 1, "children": [ { "code": null, "createtime": null, "defaultmanual": null, "description": null, "id": "b1779671ef1b45e0a9a8a1edbff03f1e", "isdelete": -1, "lastmodifytime": null, "name": "數(shù)據(jù)源服務(wù)", "parentid": "dc1d70b9eb7b4df3bbe8dcc6a93cbd57", "sort": 2, "children": [ { "code": null, "createtime": null, "defaultmanual": null, "description": null, "id": "2a38a8254ec348e9b54c9bf4622f23db", "isdelete": 1, "lastmodifytime": null, "name": "測(cè)試添加數(shù)據(jù)庫(kù)服務(wù)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": "測(cè)試添加1", "parentid": "", "sort": null, "children": [] }, { "code": null, "createtime": null, "defaultmanual": null, "description": null, "id": "62e15d859a224126884888a55df355a7", "isdelete": 1, "lastmodifytime": null, "name": "測(cè)試添加2", "parentid": "", "sort": null, "children": [] } ] }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于SpringCloud的Bus消息總線圖文詳解
這篇文章主要介紹了關(guān)于SpringCloud的Bus消息總線圖文詳解,Spring Cloud Bus是用來(lái)將分布式系統(tǒng)的節(jié)點(diǎn)與輕量級(jí)消息系統(tǒng)鏈接起來(lái)的框架,它整合了Java的事件處理機(jī)制和消息中間件的功能,需要的朋友可以參考下2023-05-05java ExecutorService CompletionService線程池區(qū)別與選擇
這篇文章主要為大家介紹了java ExecutorService CompletionService線程池區(qū)別與選擇使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Java OpenCV實(shí)現(xiàn)人臉識(shí)別過(guò)程詳解
這篇文章主要介紹了Java OpenCV實(shí)現(xiàn)人臉識(shí)別過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Java中String判斷值為null或空及地址是否相等的問(wèn)題
這篇文章主要介紹了Java中String判斷值為null或空及地址是否相等的問(wèn)題,文中舉了簡(jiǎn)單的例子對(duì)字符串類型的值和地址問(wèn)題進(jìn)行講解,需要的朋友可以參考下2016-01-01Java 內(nèi)置Http Server構(gòu)建web應(yīng)用案例詳解
這篇文章主要介紹了Java 內(nèi)置Http Server構(gòu)建web應(yīng)用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09java數(shù)據(jù)結(jié)構(gòu)之樹(shù)基本概念解析及代碼示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)之樹(shù)基本概念解析及代碼示例,介紹了樹(shù)的定義,基本術(shù)語(yǔ),主要操作及實(shí)現(xiàn)等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可了解下。2017-11-11