• JavaList集合根据ParentId递归【无限套娃】


    关键代码:

    1. public List> getChild(String pid , List allList){
    2. List> childList = new ArrayList<>();//用于保存子节点的list
    3. for(Menu ms : allList){
    4. if(pid.equals(ms.getParentId())){//判断传入的父id是否等于自身的,如果是,就说明自己是子节点
    5. Map map = new HashMap<>();
    6. map.put("id",ms.getId());
    7. map.put("pId", ms.getParentId());
    8. map.put("mate",ms.getName());
    9. map.put("hasChildren",false);
    10. map.put("ChildNodes", new Object[]{});
    11. childList.add(map); //加入子节点
    12. }
    13. }
    14. for(Map map : childList){//遍历子节点,继续递归判断每个子节点是否还含有子节点
    15. List> tList = getChild(String.valueOf(map.get("id")) , allList);
    16. if(!tList.isEmpty()){
    17. map.put("hasChildren",true);
    18. }
    19. map.put("ChildNodes" , tList);
    20. }
    21. return childList;
    22. }

    代码调用:

    1. @ResponseBody
    2. @RequestMapping(value = "authMenu", method = RequestMethod.GET)
    3. public List> authMenu(String roleId) {
    4. List list = menuService.findMenuByRoleId(roleId);
    5. List> child = getChild("0",list);
    6. return child;
    7. }

    数据结构:

    数据结果: 

    1. Object{...},
    2. {
    3. "ChildNodes":[
    4. {
    5. "ChildNodes":[
    6. ],
    7. "hasChildren":false,
    8. "name":"技术资料",
    9. "pId":"3fac53a94abd4d8786cee4fb09b59bb1",
    10. "id":"6854f03596724c589e22d6e0ede3054e"
    11. }
    12. ],
    13. "hasChildren":true,
    14. "name":"接触网",
    15. "pId":"d26688a8498045c0ac4036a4231f6a31",
    16. "id":"3fac53a94abd4d8786cee4fb09b59bb1"
    17. },
    18. Object{...},
    19. {
    20. "ChildNodes":[
    21. ],
    22. "hasChildren":false,
    23. "name":"AT所",
    24. "pId":"d26688a8498045c0ac4036a4231f6a31",
    25. "id":"1569ce89bd454485b127b5a14fd0941a"
    26. },
    27. {
    28. "ChildNodes":[
    29. ],
    30. "hasChildren":false,
    31. "name":"分区所",
    32. "pId":"d26688a8498045c0ac4036a4231f6a31",
    33. "id":"6acbab2f46e544a48596df8ae1b409b2"
    34. },
    35. {
    36. "ChildNodes":[
    37. {
    38. "ChildNodes":[
    39. {
    40. "ChildNodes":[
    41. {
    42. "ChildNodes":[
    43. ],
    44. "hasChildren":false,
    45. "name":"断链",
    46. "pId":"07f092f3578247db8cba2feb6050d8ae",
    47. "id":"64d3f7c7073148a8bb167a77daf01b24"
    48. },
    49. {
    50. "ChildNodes":[
    51. ],
    52. "hasChildren":false,
    53. "name":"曲线要素",
    54. "pId":"07f092f3578247db8cba2feb6050d8ae",
    55. "id":"ae8b1bebfbbc41a194688cf21499cb23"
    56. },
    57. {
    58. "ChildNodes":[
    59. ],
    60. "hasChildren":false,
    61. "name":"直曲",
    62. "pId":"07f092f3578247db8cba2feb6050d8ae",
    63. "id":"8deb7242334349b58049e66335a5b408"
    64. },
    65. {
    66. "ChildNodes":[
    67. ],
    68. "hasChildren":false,
    69. "name":"竖曲线要素",
    70. "pId":"07f092f3578247db8cba2feb6050d8ae",
    71. "id":"f38f88250ab54f5f8f442c66ed04a291"
    72. },
    73. {
    74. "ChildNodes":[
    75. ],
    76. "hasChildren":false,
    77. "name":"竖曲线",
    78. "pId":"07f092f3578247db8cba2feb6050d8ae",
    79. "id":"111e6ffb22ab440ba17ac7c74a5503a6"
    80. },
    81. {
    82. "ChildNodes":[
    83. ],
    84. "hasChildren":false,
    85. "name":"线间距",
    86. "pId":"07f092f3578247db8cba2feb6050d8ae",
    87. "id":"a2a189a467ac4ba8a185ad3827296a4d"
    88. },
    89. {
    90. "ChildNodes":[
    91. ],
    92. "hasChildren":false,
    93. "name":"特殊处所图例",
    94. "pId":"07f092f3578247db8cba2feb6050d8ae",
    95. "id":"f036d0c616db4cd5b4f5e599c666dfda"
    96. },
    97. {
    98. "ChildNodes":[
    99. ],
    100. "hasChildren":false,
    101. "name":"平面布置",
    102. "pId":"07f092f3578247db8cba2feb6050d8ae",
    103. "id":"3ddd21216c214de78949668472c6f8d9"
    104. }
    105. ],
    106. "hasChildren":true,
    107. "name":"基础数据",
    108. "pId":"a67ae46fe71646159e76cbe53764c4c6",
    109. "id":"07f092f3578247db8cba2feb6050d8ae"
    110. },
    111. {
    112. "ChildNodes":[
    113. ],
    114. "hasChildren":false,
    115. "name":"线路放样",
    116. "pId":"a67ae46fe71646159e76cbe53764c4c6",
    117. "id":"77a2b8e7dee44c0f8d4154a68b24824e"
    118. }
    119. ],
    120. "hasChildren":true,
    121. "name":"基础数据测量系统",
    122. "pId":"e1ccaf5a237244eda9a87bfbe335a865",
    123. "id":"a67ae46fe71646159e76cbe53764c4c6"
    124. },
    125. {
    126. "ChildNodes":[
    127. ],
    128. "hasChildren":false,
    129. "name":"一杆一档工程数据",
    130. "pId":"e1ccaf5a237244eda9a87bfbe335a865",
    131. "id":"49b03a6b78624e62a32682cad82b653a"
    132. },
    133. {
    134. "ChildNodes":[
    135. {
    136. "ChildNodes":[
    137. ],
    138. "hasChildren":false,
    139. "name":"供电段",
    140. "pId":"7f9efd8cbe734f47b9897486cb8dfb77",
    141. "id":"206a688026324bcf9e68093394c54bdb"
    142. },
    143. {
    144. "ChildNodes":[
    145. ],
    146. "hasChildren":false,
    147. "name":"车间数据",
    148. "pId":"7f9efd8cbe734f47b9897486cb8dfb77",
    149. "id":"a2dc2437f2534aae8997806cdc52211d"
    150. },
    151. {
    152. "ChildNodes":[
    153. ],
    154. "hasChildren":false,
    155. "name":"工区数据",
    156. "pId":"7f9efd8cbe734f47b9897486cb8dfb77",
    157. "id":"63e24b9f5ad541eea01a1a9929f52418"
    158. },
    159. {
    160. "ChildNodes":[
    161. ],
    162. "hasChildren":false,
    163. "name":"区间(站厂)",
    164. "pId":"7f9efd8cbe734f47b9897486cb8dfb77",
    165. "id":"adfd7fa191e84d959137aac69b79a68d"
    166. },
    167. {
    168. "ChildNodes":[
    169. ],
    170. "hasChildren":false,
    171. "name":"隧道数据",
    172. "pId":"7f9efd8cbe734f47b9897486cb8dfb77",
    173. "id":"acf44e69aa6c4cec88cf9ae48f764c95"
    174. },
    175. {
    176. "ChildNodes":[
    177. ],
    178. "hasChildren":false,
    179. "name":"桥梁数据",
    180. "pId":"7f9efd8cbe734f47b9897486cb8dfb77",
    181. "id":"0b78cd8d695b450ca0fd295bebb4e4a8"
    182. },
    183. {
    184. "ChildNodes":[
    185. ],
    186. "hasChildren":false,
    187. "name":"接触悬挂锚段",
    188. "pId":"7f9efd8cbe734f47b9897486cb8dfb77",
    189. "id":"fc4f0ebf95824757a749932ae07fe58e"
    190. },
    191. {
    192. "ChildNodes":[
    193. ],
    194. "hasChildren":false,
    195. "name":"支柱运维检修",
    196. "pId":"7f9efd8cbe734f47b9897486cb8dfb77",
    197. "id":"bc3f760b851a4b4b9548ab79aed02125"
    198. }
    199. ],
    200. "hasChildren":true,
    201. "name":"基础数据",
    202. "pId":"e1ccaf5a237244eda9a87bfbe335a865",
    203. "id":"7f9efd8cbe734f47b9897486cb8dfb77"
    204. },
    205. {
    206. "ChildNodes":[
    207. {
    208. "ChildNodes":[
    209. ],
    210. "hasChildren":false,
    211. "name":"腕臂计算输入",
    212. "pId":"e2a9904dcc6a4c27acea60ed00aef4f4",
    213. "id":"de123547a63f4a5f8112bd53f2d6a768"
    214. },
    215. {
    216. "ChildNodes":[
    217. ],
    218. "hasChildren":false,
    219. "name":"吊弦计算输入",
    220. "pId":"e2a9904dcc6a4c27acea60ed00aef4f4",
    221. "id":"f779ac39a2334de1a758252f0ab52cbc"
    222. },
    223. {
    224. "ChildNodes":[
    225. ],
    226. "hasChildren":false,
    227. "name":"腕臂计算输出",
    228. "pId":"e2a9904dcc6a4c27acea60ed00aef4f4",
    229. "id":"875d3268bc9d4ded8a09b8be8fc4ed46"
    230. },
    231. {
    232. "ChildNodes":[
    233. ],
    234. "hasChildren":false,
    235. "name":"吊弦计算输出",
    236. "pId":"e2a9904dcc6a4c27acea60ed00aef4f4",
    237. "id":"56c80ddd9bc14c618e98bf021b423ff8"
    238. }
    239. ],
    240. "hasChildren":true,
    241. "name":"接触网计算系统",
    242. "pId":"e1ccaf5a237244eda9a87bfbe335a865",
    243. "id":"e2a9904dcc6a4c27acea60ed00aef4f4"
    244. },
    245. {
    246. "ChildNodes":[
    247. {
    248. "ChildNodes":[
    249. ],
    250. "hasChildren":false,
    251. "name":"支柱信息",
    252. "pId":"ed7bfec4788e4a4ba6df826b0b642642",
    253. "id":"143bbc3e6c3e457f8e69feeea657e8c3"
    254. },
    255. {
    256. "ChildNodes":[
    257. ],
    258. "hasChildren":false,
    259. "name":"支持装置",
    260. "pId":"ed7bfec4788e4a4ba6df826b0b642642",
    261. "id":"f47dcb64ee2d4ce1a5a10b5fd215be7b"
    262. },
    263. {
    264. "ChildNodes":[
    265. ],
    266. "hasChildren":false,
    267. "name":"接触悬挂",
    268. "pId":"ed7bfec4788e4a4ba6df826b0b642642",
    269. "id":"1ba8009be7bb4b8cbb5560079fb851db"
    270. },
    271. {
    272. "ChildNodes":[
    273. ],
    274. "hasChildren":false,
    275. "name":"线路放样",
    276. "pId":"ed7bfec4788e4a4ba6df826b0b642642",
    277. "id":"f6f5efc2f57746d9bed355a703493431"
    278. }
    279. ],
    280. "hasChildren":true,
    281. "name":"线路数据驱动",
    282. "pId":"e1ccaf5a237244eda9a87bfbe335a865",
    283. "id":"ed7bfec4788e4a4ba6df826b0b642642"
    284. }
    285. ],
    286. "hasChildren":true,
    287. "name":"接触网工程",
    288. "pId":"d26688a8498045c0ac4036a4231f6a31",
    289. "id":"e1ccaf5a237244eda9a87bfbe335a865"
    290. }
    291. ],
    292. "hasChildren":true,
    293. "name":"四电工程",
    294. "pId":"0",
    295. "id":"d26688a8498045c0ac4036a4231f6a31"
    296. },
  • 相关阅读:
    drf-过滤、排序、异常处理、自封装Response
    决策树介绍
    新手教学系列——高效管理MongoDB数据:批量插入与更新的实战技巧
    从一文中了解SSRF的各种绕过姿势及攻击思路
    FPGA:基础入门LED灯闪烁
    SecurityUtils.getUser()获取为空
    为什么说Crypto游戏正在改变游戏产业?
    Webpack: 构建微前端应用
    微服务API网关
    python小玩意——图片转素描
  • 原文地址:https://blog.csdn.net/weixin_39709134/article/details/126890736