JavaList集合根据ParentId递归无限套娃
Posted java李杨勇
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaList集合根据ParentId递归无限套娃相关的知识,希望对你有一定的参考价值。
关键代码:
public List<Map<String,Object>> getChild(String pid , List<Menu> allList)
List<Map<String,Object>> childList = new ArrayList<>();//用于保存子节点的list
for(Menu ms : allList)
if(pid.equals(ms.getParentId()))//判断传入的父id是否等于自身的,如果是,就说明自己是子节点
Map<String,Object> map = new HashMap<>();
map.put("id",ms.getId());
map.put("pId", ms.getParentId());
map.put("mate",ms.getName());
map.put("hasChildren",false);
map.put("ChildNodes", new Object[]);
childList.add(map); //加入子节点
for(Map<String,Object> map : childList)//遍历子节点,继续递归判断每个子节点是否还含有子节点
List<Map<String,Object>> tList = getChild(String.valueOf(map.get("id")) , allList);
if(!tList.isEmpty())
map.put("hasChildren",true);
map.put("ChildNodes" , tList);
return childList;
代码调用:
@ResponseBody
@RequestMapping(value = "authMenu", method = RequestMethod.GET)
public List<Map<String, Object>> authMenu(String roleId)
List<Menu> list = menuService.findMenuByRoleId(roleId);
List<Map<String, Object>> child = getChild("0",list);
return child;
数据结构:
数据结果:
Object...,
"ChildNodes":[
"ChildNodes":[
],
"hasChildren":false,
"name":"技术资料",
"pId":"3fac53a94abd4d8786cee4fb09b59bb1",
"id":"6854f03596724c589e22d6e0ede3054e"
],
"hasChildren":true,
"name":"接触网",
"pId":"d26688a8498045c0ac4036a4231f6a31",
"id":"3fac53a94abd4d8786cee4fb09b59bb1"
,
Object...,
"ChildNodes":[
],
"hasChildren":false,
"name":"AT所",
"pId":"d26688a8498045c0ac4036a4231f6a31",
"id":"1569ce89bd454485b127b5a14fd0941a"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"分区所",
"pId":"d26688a8498045c0ac4036a4231f6a31",
"id":"6acbab2f46e544a48596df8ae1b409b2"
,
"ChildNodes":[
"ChildNodes":[
"ChildNodes":[
"ChildNodes":[
],
"hasChildren":false,
"name":"断链",
"pId":"07f092f3578247db8cba2feb6050d8ae",
"id":"64d3f7c7073148a8bb167a77daf01b24"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"曲线要素",
"pId":"07f092f3578247db8cba2feb6050d8ae",
"id":"ae8b1bebfbbc41a194688cf21499cb23"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"直曲",
"pId":"07f092f3578247db8cba2feb6050d8ae",
"id":"8deb7242334349b58049e66335a5b408"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"竖曲线要素",
"pId":"07f092f3578247db8cba2feb6050d8ae",
"id":"f38f88250ab54f5f8f442c66ed04a291"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"竖曲线",
"pId":"07f092f3578247db8cba2feb6050d8ae",
"id":"111e6ffb22ab440ba17ac7c74a5503a6"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"线间距",
"pId":"07f092f3578247db8cba2feb6050d8ae",
"id":"a2a189a467ac4ba8a185ad3827296a4d"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"特殊处所图例",
"pId":"07f092f3578247db8cba2feb6050d8ae",
"id":"f036d0c616db4cd5b4f5e599c666dfda"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"平面布置",
"pId":"07f092f3578247db8cba2feb6050d8ae",
"id":"3ddd21216c214de78949668472c6f8d9"
],
"hasChildren":true,
"name":"基础数据",
"pId":"a67ae46fe71646159e76cbe53764c4c6",
"id":"07f092f3578247db8cba2feb6050d8ae"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"线路放样",
"pId":"a67ae46fe71646159e76cbe53764c4c6",
"id":"77a2b8e7dee44c0f8d4154a68b24824e"
],
"hasChildren":true,
"name":"基础数据测量系统",
"pId":"e1ccaf5a237244eda9a87bfbe335a865",
"id":"a67ae46fe71646159e76cbe53764c4c6"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"一杆一档工程数据",
"pId":"e1ccaf5a237244eda9a87bfbe335a865",
"id":"49b03a6b78624e62a32682cad82b653a"
,
"ChildNodes":[
"ChildNodes":[
],
"hasChildren":false,
"name":"供电段",
"pId":"7f9efd8cbe734f47b9897486cb8dfb77",
"id":"206a688026324bcf9e68093394c54bdb"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"车间数据",
"pId":"7f9efd8cbe734f47b9897486cb8dfb77",
"id":"a2dc2437f2534aae8997806cdc52211d"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"工区数据",
"pId":"7f9efd8cbe734f47b9897486cb8dfb77",
"id":"63e24b9f5ad541eea01a1a9929f52418"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"区间(站厂)",
"pId":"7f9efd8cbe734f47b9897486cb8dfb77",
"id":"adfd7fa191e84d959137aac69b79a68d"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"隧道数据",
"pId":"7f9efd8cbe734f47b9897486cb8dfb77",
"id":"acf44e69aa6c4cec88cf9ae48f764c95"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"桥梁数据",
"pId":"7f9efd8cbe734f47b9897486cb8dfb77",
"id":"0b78cd8d695b450ca0fd295bebb4e4a8"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"接触悬挂锚段",
"pId":"7f9efd8cbe734f47b9897486cb8dfb77",
"id":"fc4f0ebf95824757a749932ae07fe58e"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"支柱运维检修",
"pId":"7f9efd8cbe734f47b9897486cb8dfb77",
"id":"bc3f760b851a4b4b9548ab79aed02125"
],
"hasChildren":true,
"name":"基础数据",
"pId":"e1ccaf5a237244eda9a87bfbe335a865",
"id":"7f9efd8cbe734f47b9897486cb8dfb77"
,
"ChildNodes":[
"ChildNodes":[
],
"hasChildren":false,
"name":"腕臂计算输入",
"pId":"e2a9904dcc6a4c27acea60ed00aef4f4",
"id":"de123547a63f4a5f8112bd53f2d6a768"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"吊弦计算输入",
"pId":"e2a9904dcc6a4c27acea60ed00aef4f4",
"id":"f779ac39a2334de1a758252f0ab52cbc"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"腕臂计算输出",
"pId":"e2a9904dcc6a4c27acea60ed00aef4f4",
"id":"875d3268bc9d4ded8a09b8be8fc4ed46"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"吊弦计算输出",
"pId":"e2a9904dcc6a4c27acea60ed00aef4f4",
"id":"56c80ddd9bc14c618e98bf021b423ff8"
],
"hasChildren":true,
"name":"接触网计算系统",
"pId":"e1ccaf5a237244eda9a87bfbe335a865",
"id":"e2a9904dcc6a4c27acea60ed00aef4f4"
,
"ChildNodes":[
"ChildNodes":[
],
"hasChildren":false,
"name":"支柱信息",
"pId":"ed7bfec4788e4a4ba6df826b0b642642",
"id":"143bbc3e6c3e457f8e69feeea657e8c3"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"支持装置",
"pId":"ed7bfec4788e4a4ba6df826b0b642642",
"id":"f47dcb64ee2d4ce1a5a10b5fd215be7b"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"接触悬挂",
"pId":"ed7bfec4788e4a4ba6df826b0b642642",
"id":"1ba8009be7bb4b8cbb5560079fb851db"
,
"ChildNodes":[
],
"hasChildren":false,
"name":"线路放样",
"pId":"ed7bfec4788e4a4ba6df826b0b642642",
"id":"f6f5efc2f57746d9bed355a703493431"
],
"hasChildren":true,
"name":"线路数据驱动",
"pId":"e1ccaf5a237244eda9a87bfbe335a865",
"id":"ed7bfec4788e4a4ba6df826b0b642642"
],
"hasChildren":true,
"name":"接触网工程",
"pId":"d26688a8498045c0ac4036a4231f6a31",
"id":"e1ccaf5a237244eda9a87bfbe335a865"
],
"hasChildren":true,
"name":"四电工程",
"pId":"0",
"id":"d26688a8498045c0ac4036a4231f6a31"
,
以上是关于JavaList集合根据ParentId递归无限套娃的主要内容,如果未能解决你的问题,请参考以下文章