java中实现无限层级的树形结构
本文展示了两个实现方法的代码。两个代码的实现方法不同,代码2更为简单。
先看一下最后实现的结果:
最后结果-json
代码1:
实现过程:
1.传入一段json字符串
2.将字符串转换成对象存入节点列表
3.根据节点列表构造无序的多叉树,并将个孩子节点加入对应的父节点中,将无对应父节点的节点加入一级节点列表
4.排序
代码:
1 package com.example.rabbitmq2; 2 3 4 5 import com.alibaba.fastjson.JSON; 6 import com.alibaba.fastjson.JSONArray; 7 import com.alibaba.fastjson.JSONObject; 8 9 import java.util.*; 10 11 public class MultipleTree { 12 public static void main(String[] args) { 13 14 /** 15 * 读取层次数据结果集列表 16 */ 17 String strJson ="[\n" +"" + 18 " {\n" + 19 " \"id\":50090113,\n" + 20 " \"parentId\":50000001,\n" + 21 " \"name\":\"应用技术研发二中心\",\n" + 22 " \"status\":0, \n" + 23 " \"fullName\":\"应用技术研发二中心\"\n" + 24 " },\n" + 25 " {\n" + 26 " \"id\":50090193,\n" + 27 " \"parentId\":50000000,\n" + 28 " \"name\":\"应用技术研发一中心\",\n" + 29 " \"status\":0, \n" + 30 " \"fullName\":\"应用技术研发一中心\"\n" + 31 " },\n" + 32 " {\n" + 33 " \"id\":50090194,\n" + 34 " \"parentId\":50090193,\n" + 35 " \"name\":\"前端研发部\",\n" + 36 " \"status\":0,\n" + 37 " \"fullName\":\"应用技术研发一中心/前端研发部\"\n" + 38 " },\n" + 39 " {\n" + 40 " \"id\":50005413,\n" + 41 " \"parentId\":50090194,\n" + 42 " \"name\":\"安全网关产品前端研发组\",\n" + 43 " \"status\":1,\n" + 44 " \"fullName\":\"应用技术研发中心/前端研发部/安全网关产品前端研发组\"\n" + 45 " },\n" + 46 " {\n" + 47 " \"id\":50005415,\n" + 48 " \"parentId\":50090194,\n" + 49 " \"name\":\"前端研发二组\",\n" + 50 " \"status\":1,\n" + 51 " \"fullName\":\"应用技术研发中心/前端研发部/前端研发二组\"\n" + 52 " },\n" + 53 " {\n" + 54 " \"id\":50090199,\n" + 55 " \"parentId\":50090194,\n" + 56 " \"name\":\"云安全前端研发组\",\n" + 57 " \"status\":1,\n" + 58 " \"fullName\":\"应用技术研发中心/前端研发部/云安全前端研发组\"\n" + 59 " },\n" + 60 " {\n" + 61 " \"id\":50090200,\n" + 62 " \"parentId\":50090194,\n" + 63 " \"name\":\"威胁情报前端研发组\",\n" + 64 " \"status\":1,\n" + 65 " \"fullName\":\"应用技术研发中心/前端研发部/威胁情报前端研发组\"\n" + 66 " },\n" + 67 " {\n" + 68 " \"id\":50090201,\n" + 69 " \"parentId\":50090194,\n" + 70 " \"name\":\"大数据与安全运营前端产品组\",\n" + 71 " \"status\":1,\n" + 72 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端产品组\"\n" + 73 " \n" + 74 " },\n" + 75 " {\n" + 76 " \"id\":50090202,\n" + 77 " \"parentId\":50090194,\n" + 78 " \"name\":\"大数据与安全运营前端架构组\",\n" + 79 " \"status\":1,\n" + 80 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端架构组\"\n" + 81 " \n" + 82 " },\n" + 83 " {\n" + 84 " \"id\":50090203,\n" + 85 " \"parentId\":50090194,\n" + 86 " \"name\":\"大数据与安全运营前端研发二组\",\n" + 87 " \"status\":1,\n" + 88 " \n" + 89 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发二组\"\n" + 90 " },\n" + 91 " {\n" + 92 " \"id\":50090204,\n" + 93 " \"parentId\":50090194,\n" + 94 " \"name\":\"大数据与安全运营前端研发三组\",\n" + 95 " \"status\":1,\n" + 96 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发三组\"\n" + 97 "\n" + 98 " },\n" + 99 " {\n" + 100 " \"id\":50090205,\n" + 101 " \"parentId\":50090194,\n" + 102 " \"name\":\"大数据与安全运营前端研发四组\",\n" + 103 " \"status\":1,\n" + 104 " \n" + 105 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发四组\"\n" + 106 "\n" + 107 " },\n" + 108 " {\n" + 109 " \"id\":50090206,\n" + 110 " \"parentId\":50090194,\n" + 111 " \"name\":\"大数据与安全运营前端研发运营商部\",\n" + 112 " \"status\":1,\n" + 113 " \n" + 114 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发运营商部\"\n" + 115 "\n" + 116 " },\n" + 117 " {\n" + 118 " \"id\":50090207,\n" + 119 " \"parentId\":50090194,\n" + 120 " \"name\":\"大数据与安全运营前端研发政企部\",\n" + 121 " \"status\":1, \n" + 122 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发政企部\"\n" + 123 " },\n" + 124 " {\n" + 125 " \"id\":50090208,\n" + 126 " \"parentId\":50090194,\n" + 127 " \"name\":\"态势感知前端研发组\",\n" + 128 " \"status\":1,\n" + 129 " \"fullName\":\"应用技术研发中心/前端研发部/态势感知前端研发组\"\n" + 130 " },\n" + 131 " {\n" + 132 " \"id\":50090209,\n" + 133 " \"parentId\":50090194,\n" + 134 " \"name\":\"大数据应用前端研发组\",\n" + 135 " \"status\":1,\n" + 136 " \"fullName\":\"应用技术研发中心/前端研发部/大数据应用前端研发组\"\n" + 137 " },\n" + 138 " {\n" + 139 " \"id\":50090210,\n" + 140 " \"parentId\":50090194,\n" + 141 " \"name\":\"大数据与威胁分析前端研发一组\",\n" + 142 " \"status\":1,\n" + 143 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁分析前端研发一组\"\n" + 144 " },\n" + 145 " {\n" + 146 " \"id\":50090211,\n" + 147 " \"parentId\":50090194,\n" + 148 " \"name\":\"大数据与威胁公司前端研发二组\",\n" + 149 " \"status\":1,\n" + 150 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁公司前端研发二组\"\n" + 151 " },\n" + 152 " {\n" + 153 " \"id\":50090212,\n" + 154 " \"parentId\":50090194,\n" + 155 " \"name\":\"大数据与威胁分析前端研发三组\",\n" + 156 " \"status\":1,\n" + 157 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁分析前端研发三组\"\n" + 158 " },\n" + 159 " {\n" + 160 " \"id\":50090213,\n" + 161 " \"parentId\":50090194,\n" + 162 " \"name\":\"工业安全前端研发组\",\n" + 163 " \"status\":1,\n" + 164 " \"fullName\":\"应用技术研发中心/前端研发部/工业安全前端研发组\"\n" + 165 " },\n" + 166 " {\n" + 167 " \"id\":50090214,\n" + 168 " \"parentId\":50090194,\n" + 169 " \"name\":\"行为安全前端研发组\",\n" + 170 " \"status\":1,\n" + 171 " \"fullName\":\"应用技术研发中心/前端研发部/行为安全前端研发组\"\n" + 172 " },\n" + 173 " {\n" + 174 " \"id\":50090215,\n" + 175 " \"parentId\":50090194,\n" + 176 " \"name\":\"基础大数据前端研发组\",\n" + 177 " \"status\":1,\n" + 178 " \"fullName\":\"应用技术研发中心/前端研发部/基础大数据前端研发组\"\n" + 179 " },\n" + 180 " {\n" + 181 " \"id\":50090216,\n" + 182 " \"parentId\":50090194,\n" + 183 " \"name\":\"应用产品前端研发组\",\n" + 184 " \"status\":1,\n" + 185 " \"fullName\":\"应用技术研发中心/前端研发部/应用产品前端研发组\"\n" + 186 " },\n" + 187 " {\n" + 188 " \"id\":50090217,\n" + 189 " \"parentId\":50090194,\n" + 190 " \"name\":\"终端安全产品前端研发组\",\n" + 191 " \"status\":0,\n" + 192 " \"fullName\":\"应用技术研发一中心/前端研发部/终端安全产品前端研发组\"\n" + 193 " },\n" + 194 " {\n" + 195 " \"id\":50090218,\n" + 196 " \"parentId\":50090194,\n" + 197 " \"name\":\"安全产品前端研发组\",\n" + 198 " \"status\":1,\n" + 199 " \"fullName\":\"应用技术研发中心/前端研发部/安全产品前端研发组\"\n" + 200 " },\n" + 201 " {\n" + 202 " \"id\":50090219,\n" + 203 " \"parentId\":50090194,\n" + 204 " \"name\":\"数据安全前端研发组\",\n" + 205 " \"status\":0,\n" + 206 " \"fullName\":\"应用技术研发一中心/前端研发部/数据安全前端研发组\"\n" + 207 " },\n" + 208 " {\n" + 209 " \"id\":50090220,\n" + 210 " \"parentId\":50090194,\n" + 211 " \"name\":\"运维前端研发组\",\n" + 212 " \"status\":1,\n" + 213 " \"fullName\":\"应用技术研发中心/前端研发部/运维前端研发组\"\n" + 214 " },\n" + 215 " {\n" + 216 " \"id\":50090221,\n" + 217 " \"parentId\":50090194,\n" + 218 " \"name\":\"华南基地前端研发组\",\n" + 219 " \"status\":1,\n" + 220 " \"fullName\":\"应用技术研发中心/前端研发部/华南基地前端研发组\"\n" + 221 " },\n" + 222 " {\n" + 223 " \"id\":50090222,\n" + 224 " \"parentId\":50090194,\n" + 225 " \"name\":\"补天产品前端研发部\",\n" + 226 " \"status\":1,\n" + 227 " \"fullName\":\"应用技术研发中心/前端研发部/补天产品前端研发部\"\n" + 228 " },\n" + 229 " {\n" + 230 " \"id\":50090789,\n" + 231 " \"parentId\":50090194,\n" + 232 " \"name\":\"前端一部\",\n" + 233 " \"status\":0,\n" + 234 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部\"\n" + 235 " },\n" + 236 " {\n" + 237 " \"id\":50090790,\n" + 238 " \"parentId\":50090789,\n" + 239 " \"name\":\"前端一组\",\n" + 240 " \"status\":0,\n" + 241 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/前端一组\"\n" + 242 " },\n" + 243 " {\n" + 244 " \"id\":50090795,\n" + 245 " \"parentId\":50090789,\n" + 246 " \"name\":\"前端三组\",\n" + 247 " \"status\":0,\n" + 248 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/前端三组\"\n" + 249 " },\n" + 250 " {\n" + 251 " \"id\":50090797,\n" + 252 " \"parentId\":50090789,\n" + 253 " \"name\":\"珠海组\",\n" + 254 " \"status\":0,\n" + 255 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/珠海组\"\n" + 256 " },\n" + 257 " {\n" + 258 " \"id\":50091815,\n" + 259 " \"parentId\":50090789,\n" + 260 " \"name\":\"西安组\",\n" + 261 " \"status\":0,\n" + 262 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/西安组\"\n" + 263 " },\n" + 264 " {\n" + 265 " \"id\":50090793,\n" + 266 " \"parentId\":50090194,\n" + 267 " \"name\":\"前端二部\",\n" + 268 " \"status\":0,\n" + 269 " \"fullName\":\"应用技术研发一中心/前端研发部/前端二部\"\n" + 270 " },\n" + 271 " {\n" + 272 " \"id\":50090791,\n" + 273 " \"parentId\":50090793,\n" + 274 " \"name\":\"前端四组\",\n" + 275 " \"status\":0,\n" + 276 " \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/前端四组\"\n" + 277 " },\n" + 278 " {\n" + 279 " \"id\":50090794,\n" + 280 " \"parentId\":50090793,\n" + 281 " \"name\":\"前端二组\",\n" + 282 " \"status\":0,\n" + 283 "\n" + 284 " \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/前端二组\"\n" + 285 " },\n" + 286 " {\n" + 287 " \"id\":50091816,\n" + 288 " \"parentId\":50090793,\n" + 289 " \"name\":\"上海组\",\n" + 290 " \"status\":0,\n" + 291 " \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/上海组\"\n" + 292 " },\n" + 293 " {\n" + 294 " \"id\":50090798,\n" + 295 " \"parentId\":50090194,\n" + 296 " \"name\":\"共性技术组\",\n" + 297 " \"status\":0,\n" + 298 " \"fullName\":\"应用技术研发一中心/前端研发部/共性技术组\"\n" + 299 " },\n" + 300 " {\n" + 301 " \"id\":50091814,\n" + 302 " \"parentId\":50090194,\n" + 303 " \"name\":\"前端三部\",\n" + 304 " \"status\":0,\n" + 305 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部\"\n" + 306 " },\n" + 307 " {\n" + 308 " \"id\":50090792,\n" + 309 " \"parentId\":50091814,\n" + 310 " \"name\":\"前端六组\",\n" + 311 " \"status\":0,\n" + 312 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端六组\"\n" + 313 " },\n" + 314 " {\n" + 315 " \"id\":50090796,\n" + 316 " \"parentId\":50091814,\n" + 317 " \"name\":\"前端七组\",\n" + 318 " \"status\":0,\n" + 319 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端七组\"\n" + 320 " },\n" + 321 " {\n" + 322 " \"id\":50091817,\n" + 323 " \"parentId\":50091814,\n" + 324 " \"name\":\"前端八组\",\n" + 325 " \"status\":0,\n" + 326 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端八组\"\n" + 327 " },\n" + 328 " {\n" + 329 " \"id\":50091818,\n" + 330 " \"parentId\":50091814,\n" + 331 " \"name\":\"前端九组\",\n" + 332 " \"status\":0,\n" + 333 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端九组\"\n" + 334 " },\n" + 335 " {\n" + 336 " \"id\":50090195,\n" + 337 " \"parentId\":50090193,\n" + 338 " \"name\":\"研发一部\",\n" + 339 " \"status\":0,\n" + 340 " \"fullName\":\"应用技术研发一中心/研发一部\"\n" + 341 " },\n" + 342 " {\n" + 343 " \"id\":50090620,\n" + 344 " \"parentId\":50090195,\n" + 345 " \"name\":\"策略管理组\",\n" + 346 " \"status\":0,\n" + 347 " \"fullName\":\"应用技术研发一中心/研发一部/策略管理组\"\n" + 348 " },\n" + 349 " {\n" + 350 " \"id\":50090621,\n" + 351 " \"parentId\":50090195,\n" + 352 " \"name\":\"集中管理组\",\n" + 353 " \"status\":0,\n" + 354 " \"fullName\":\"应用技术研发一中心/研发一部/集中管理组\"\n" + 355 " },\n" + 356 " {\n" + 357 " \"id\":50090622,\n" + 358 " \"parentId\":50090195,\n" + 359 " \"name\":\"产品规划与设计组\",\n" + 360 " \"status\":0,\n" + 361 " \"fullName\":\"应用技术研发一中心/研发一部/产品规划与设计组\"\n" + 362 " },\n" + 363 " {\n" + 364 " \"id\":50090632,\n" + 365 " \"parentId\":50090195,\n" + 366 " \"name\":\"研发管理组\",\n" + 367 " \"status\":0,\n" + 368 " \"fullName\":\"应用技术研发一中心/研发一部/研发管理组\"\n" + 369 " },\n" + 370 " {\n" + 371 " \"id\":50091022,\n" + 372 " \"parentId\":50090195,\n" + 373 " \"name\":\"解决方案组\",\n" + 374 " \"status\":0,\n" + 375 " \"fullName\":\"应用技术研发一中心/研发一部/解决方案组\"\n" + 376 " },\n" + 377 " {\n" + 378 " \"id\":50091023,\n" + 379 " \"parentId\":50090195,\n" + 380 " \"name\":\"公共服务组\",\n" + 381 " \"status\":0,\n" + 382 " \"fullName\":\"应用技术研发一中心/研发一部/公共服务组\"\n" + 383 "\n" + 384 " },\n" + 385 " {\n" + 386 " \"id\":50091024,\n" + 387 " \"parentId\":50090195,\n" + 388 " \"name\":\"网络拓普组\",\n" + 389 " \"status\":0,\n" + 390 " \"fullName\":\"应用技术研发一中心/研发一部/网络拓普组\"\n" + 391 " },\n" + 392 " {\n" + 393 " \"id\":50090197,\n" + 394 " \"parentId\":50090193,\n" + 395 " \"name\":\"研发三部\",\n" + 396 " \"status\":0,\n" + 397 " \"fullName\":\"应用技术研发一中心/研发三部\"\n" + 398 " },\n" + 399 " {\n" + 400 " \"id\":50090232,\n" + 401 " \"parentId\":50090197,\n" + 402 " \"name\":\"工业安全服务端研发组\",\n" + 403 " \"status\":0,\n" + 404 " \"fullName\":\"应用技术研发一中心/研发三部/工业安全服务端研发组\"\n" + 405 " },\n" + 406 " {\n" + 407 " \"id\":50090631,\n" + 408 " \"parentId\":50090193,\n" + 409 " \"name\":\"设计与研发支撑部\",\n" + 410 " \"status\":0,\n" + 411 " \"fullName\":\"应用技术研发一中心/设计与研发支撑部\"\n" + 412 " },\n" + 413 " {\n" + 414 " \"id\":50090633,\n" + 415 " \"parentId\":50090193,\n" + 416 " \"name\":\"UED\",\n" + 417 " \"status\":0,\n" + 418 " \"fullName\":\"应用技术研发一中心/UED\"\n" + 419 " },\n" + 420 " {\n" + 421 " \"id\":50090799,\n" + 422 " \"parentId\":50090193,\n" + 423 " \"name\":\"武汉研发部\",\n" + 424 " \"status\":0,\n" + 425 " \"fullName\":\"应用技术研发一中心/武汉研发部\"\n" + 426 " },\n" + 427 " {\n" + 428 " \"id\":50090800,\n" + 429 " \"parentId\":50090799,\n" + 430 " \"name\":\"武汉前端一组\",\n" + 431 " \"status\":0,\n" + 432 " \"fullName\":\"应用技术研发一中心/武汉研发部/武汉前端一组\"\n" + 433 " },\n" + 434 " {\n" + 435 " \"id\":50090801,\n" + 436 " \"parentId\":50090799,\n" + 437 " \"name\":\"武汉前端二组\",\n" + 438 " \"status\":0,\n" + 439 " \"fullName\":\"应用技术研发一中心/武汉研发部/武汉前端二组\"\n" + 440 " }]\n"; 441 //字符串转换JSON数组 442 JSONArray jsonArray = JSONArray.parseArray(strJson); 443 //节点列表(散列表,用于临时存储节点对象) 444 HashMap nodeList = new HashMap(); 445 //最后结果节点列表 446 HashMap nodeResultList = new HashMap(); 447 //根节点 448 Node root = new Node(); 449 Node node =new Node(); 450 //设置根节点,id为0 451 nodeResultList.put(0, node); 452 453 //根据结果集构造节点列表(存入散列表) 454 for(int i=0;i<jsonArray.size();i++){ 455 Node node1 = JSONObject.parseObject(jsonArray.get(i).toString(),Node.class); 456 nodeList.put(node1.id, node1); 457 } 458 459 //构造无序的多叉树 //entrySet() 该方法返回值是这个map中各个键值对映射关系的集合 460 Set entrySet = nodeList.entrySet(); 461 //value=entrySet.iterator().next().getValue() key=entrySet.iterator().next().getKey() 462 for (Iterator it = entrySet.iterator(); it.hasNext();) { 463 node = (Node) ((Map.Entry) it.next()).getValue(); 464 int i= 1; 465 //如果parentId有对应的id,则作为孩子节点加入对应的父节点中 466 for (Iterator it2 = entrySet.iterator(); it2.hasNext();) { 467 Node node2 = (Node) ((Map.Entry) it2.next()).getValue(); 468 if (node.parentId == node2.id) { 469 ((Node) nodeList.get(node.parentId)).addChild(node); 470 System.out.println(node.parentId); 471 i++; 472 break; 473 } 474 } 475 //若parentId都无对应的id,则为一级节点,加入根节点中 476 if(i==1){ 477 ((Node) nodeResultList.get(0)).addChild(node); 478 } 479 } 480 481 root = (Node) nodeResultList.get(0); 482 // 输出无序的树形菜单的JSON字符串 483 System.out.println(JSON.toJSONString(root)); 484 // 对多叉树进行横向排序 485 root.sortChildren(); 486 487 // 输出有序的树形菜单的JSON字符串 488 //重写的toString方法的结果,(可使最后结果根据自己想要展示的样子展示) 489 System.out.println("------------------root.toString()"+root.toString()); 490 //未重写toString方法的结果 491 System.out.println("------------------JSON.toJSONString(root)"+JSON.toJSONString(root)); 492 493 } 494 }
1 package com.example.rabbitmq2; 2 3 import com.alibaba.fastjson.JSONObject; 4 import lombok.Data; 5 import lombok.Getter; 6 import lombok.Setter; 7 8 import java.util.ArrayList; 9 import java.util.Collections; 10 import java.util.Iterator; 11 import java.util.List; 12 13 @Data 14 public class Node { 15 /** 16 * 节点编号 17 */ 18 public int id; 19 /** 20 * 父节点编号 21 */ 22 public int parentId; 23 /** 24 * 节点名称 25 */ 26 public String name; 27 /** 28 * 节点状态 29 */ 30 public int status; 31 /** 32 * 节点全称 33 */ 34 public String fullName; 35 36 /** 37 * 孩子节点列表 38 */ 39 private List childrenList = new ArrayList(); 40 41 // 先序遍历,拼接JSON字符串 42 public String toString() { 43 String result = "{" 44 + "\"id\" : \"" + id + "\"" 45 + ", \"fullName\" : \"" + fullName + "\"" 46 + ", \"name\" : \"" + name + "\"" 47 + ", \"status\" : \"" + status + "\""; 48 49 if (childrenList != null && childrenList.size() != 0) { 50 result += ", \"children\" : " + childrenList.toString(); 51 } 52 return result + "}"; 53 } 54 55 // 兄弟节点横向排序 56 public void sortChildren() { 57 if (childrenList != null && childrenList.size() != 0) { 58 // 对本层节点进行排序 59 // 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器 60 Collections.sort(childrenList, new NodeIDComparator()); 61 // 对每个节点的下一层节点进行排序 62 for (Iterator it = childrenList.iterator(); it.hasNext();) { 63 ((Node) it.next()).sortChildren(); 64 } 65 } 66 } 67 // 添加孩子节点 68 public void addChild(Node node) { 69 childrenList.add(node); 70 71 } 72 }
1 package com.example.rabbitmq2; 2 3 import java.util.Comparator; 4 5 /** 6 * 节点比较器 7 */ 8 class NodeIDComparator implements Comparator { 9 // 按照节点编号比较 10 public int compare(Object o1, Object o2) { 11 int j1 = ((Node) o1).id; 12 int j2 = ((Node) o2).id; 13 return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1)); 14 } 15 }
代码2:
实现过程:
1.将传入的json字符串转为List
2.找的所有的一级节点存入结果list中
3.使用递归,为所有的节点设置子节点
代码
1 package com.example.rabbitmq2; 2 3 import com.alibaba.fastjson.JSON; 4 import com.alibaba.fastjson.JSONArray; 5 import com.alibaba.fastjson.JSONObject; 6 import org.junit.platform.commons.util.StringUtils; 7 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 13 public class MultipleTree3 { 14 public static void main(String[] args) { 15 // 原始的数据 16 /** 17 * 读取层次数据结果集列表 18 */ 19 String strJson ="[\n" +"" + 20 " {\n" + 21 " \"id\":50090113,\n" + 22 " \"parentId\":50000001,\n" + 23 " \"name\":\"应用技术研发二中心\",\n" + 24 " \"status\":0, \n" + 25 " \"fullName\":\"应用技术研发二中心\"\n" + 26 " },\n" + 27 " {\n" + 28 " \"id\":50090193,\n" + 29 " \"parentId\":50000000,\n" + 30 " \"name\":\"应用技术研发一中心\",\n" + 31 " \"status\":0, \n" + 32 " \"fullName\":\"应用技术研发一中心\"\n" + 33 " },\n" + 34 " {\n" + 35 " \"id\":50090194,\n" + 36 " \"parentId\":50090193,\n" + 37 " \"name\":\"前端研发部\",\n" + 38 " \"status\":0,\n" + 39 " \"fullName\":\"应用技术研发一中心/前端研发部\"\n" + 40 " },\n" + 41 " {\n" + 42 " \"id\":50005413,\n" + 43 " \"parentId\":50090194,\n" + 44 " \"name\":\"安全网关产品前端研发组\",\n" + 45 " \"status\":1,\n" + 46 " \"fullName\":\"应用技术研发中心/前端研发部/安全网关产品前端研发组\"\n" + 47 " },\n" + 48 " {\n" + 49 " \"id\":50005415,\n" + 50 " \"parentId\":50090194,\n" + 51 " \"name\":\"前端研发二组\",\n" + 52 " \"status\":1,\n" + 53 " \"fullName\":\"应用技术研发中心/前端研发部/前端研发二组\"\n" + 54 " },\n" + 55 " {\n" + 56 " \"id\":50090199,\n" + 57 " \"parentId\":50090194,\n" + 58 " \"name\":\"云安全前端研发组\",\n" + 59 " \"status\":1,\n" + 60 " \"fullName\":\"应用技术研发中心/前端研发部/云安全前端研发组\"\n" + 61 " },\n" + 62 " {\n" + 63 " \"id\":50090200,\n" + 64 " \"parentId\":50090194,\n" + 65 " \"name\":\"威胁情报前端研发组\",\n" + 66 " \"status\":1,\n" + 67 " \"fullName\":\"应用技术研发中心/前端研发部/威胁情报前端研发组\"\n" + 68 " },\n" + 69 " {\n" + 70 " \"id\":50090201,\n" + 71 " \"parentId\":50090194,\n" + 72 " \"name\":\"大数据与安全运营前端产品组\",\n" + 73 " \"status\":1,\n" + 74 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端产品组\"\n" + 75 " \n" + 76 " },\n" + 77 " {\n" + 78 " \"id\":50090202,\n" + 79 " \"parentId\":50090194,\n" + 80 " \"name\":\"大数据与安全运营前端架构组\",\n" + 81 " \"status\":1,\n" + 82 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端架构组\"\n" + 83 " \n" + 84 " },\n" + 85 " {\n" + 86 " \"id\":50090203,\n" + 87 " \"parentId\":50090194,\n" + 88 " \"name\":\"大数据与安全运营前端研发二组\",\n" + 89 " \"status\":1,\n" + 90 " \n" + 91 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发二组\"\n" + 92 " },\n" + 93 " {\n" + 94 " \"id\":50090204,\n" + 95 " \"parentId\":50090194,\n" + 96 " \"name\":\"大数据与安全运营前端研发三组\",\n" + 97 " \"status\":1,\n" + 98 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发三组\"\n" + 99 "\n" + 100 " },\n" + 101 " {\n" + 102 " \"id\":50090205,\n" + 103 " \"parentId\":50090194,\n" + 104 " \"name\":\"大数据与安全运营前端研发四组\",\n" + 105 " \"status\":1,\n" + 106 " \n" + 107 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发四组\"\n" + 108 "\n" + 109 " },\n" + 110 " {\n" + 111 " \"id\":50090206,\n" + 112 " \"parentId\":50090194,\n" + 113 " \"name\":\"大数据与安全运营前端研发运营商部\",\n" + 114 " \"status\":1,\n" + 115 " \n" + 116 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发运营商部\"\n" + 117 "\n" + 118 " },\n" + 119 " {\n" + 120 " \"id\":50090207,\n" + 121 " \"parentId\":50090194,\n" + 122 " \"name\":\"大数据与安全运营前端研发政企部\",\n" + 123 " \"status\":1, \n" + 124 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发政企部\"\n" + 125 " },\n" + 126 " {\n" + 127 " \"id\":50090208,\n" + 128 " \"parentId\":50090194,\n" + 129 " \"name\":\"态势感知前端研发组\",\n" + 130 " \"status\":1,\n" + 131 " \"fullName\":\"应用技术研发中心/前端研发部/态势感知前端研发组\"\n" + 132 " },\n" + 133 " {\n" + 134 " \"id\":50090209,\n" + 135 " \"parentId\":50090194,\n" + 136 " \"name\":\"大数据应用前端研发组\",\n" + 137 " \"status\":1,\n" + 138 " \"fullName\":\"应用技术研发中心/前端研发部/大数据应用前端研发组\"\n" + 139 " },\n" + 140 " {\n" + 141 " \"id\":50090210,\n" + 142 " \"parentId\":50090194,\n" + 143 " \"name\":\"大数据与威胁分析前端研发一组\",\n" + 144 " \"status\":1,\n" + 145 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁分析前端研发一组\"\n" + 146 " },\n" + 147 " {\n" + 148 " \"id\":50090211,\n" + 149 " \"parentId\":50090194,\n" + 150 " \"name\":\"大数据与威胁公司前端研发二组\",\n" + 151 " \"status\":1,\n" + 152 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁公司前端研发二组\"\n" + 153 " },\n" + 154 " {\n" + 155 " \"id\":50090212,\n" + 156 " \"parentId\":50090194,\n" + 157 " \"name\":\"大数据与威胁分析前端研发三组\",\n" + 158 " \"status\":1,\n" + 159 " \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁分析前端研发三组\"\n" + 160 " },\n" + 161 " {\n" + 162 " \"id\":50090213,\n" + 163 " \"parentId\":50090194,\n" + 164 " \"name\":\"工业安全前端研发组\",\n" + 165 " \"status\":1,\n" + 166 " \"fullName\":\"应用技术研发中心/前端研发部/工业安全前端研发组\"\n" + 167 " },\n" + 168 " {\n" + 169 " \"id\":50090214,\n" + 170 " \"parentId\":50090194,\n" + 171 " \"name\":\"行为安全前端研发组\",\n" + 172 " \"status\":1,\n" + 173 " \"fullName\":\"应用技术研发中心/前端研发部/行为安全前端研发组\"\n" + 174 " },\n" + 175 " {\n" + 176 " \"id\":50090215,\n" + 177 " \"parentId\":50090194,\n" + 178 " \"name\":\"基础大数据前端研发组\",\n" + 179 " \"status\":1,\n" + 180 " \"fullName\":\"应用技术研发中心/前端研发部/基础大数据前端研发组\"\n" + 181 " },\n" + 182 " {\n" + 183 " \"id\":50090216,\n" + 184 " \"parentId\":50090194,\n" + 185 " \"name\":\"应用产品前端研发组\",\n" + 186 " \"status\":1,\n" + 187 " \"fullName\":\"应用技术研发中心/前端研发部/应用产品前端研发组\"\n" + 188 " },\n" + 189 " {\n" + 190 " \"id\":50090217,\n" + 191 " \"parentId\":50090194,\n" + 192 " \"name\":\"终端安全产品前端研发组\",\n" + 193 " \"status\":0,\n" + 194 " \"fullName\":\"应用技术研发一中心/前端研发部/终端安全产品前端研发组\"\n" + 195 " },\n" + 196 " {\n" + 197 " \"id\":50090218,\n" + 198 " \"parentId\":50090194,\n" + 199 " \"name\":\"安全产品前端研发组\",\n" + 200 " \"status\":1,\n" + 201 " \"fullName\":\"应用技术研发中心/前端研发部/安全产品前端研发组\"\n" + 202 " },\n" + 203 " {\n" + 204 " \"id\":50090219,\n" + 205 " \"parentId\":50090194,\n" + 206 " \"name\":\"数据安全前端研发组\",\n" + 207 " \"status\":0,\n" + 208 " \"fullName\":\"应用技术研发一中心/前端研发部/数据安全前端研发组\"\n" + 209 " },\n" + 210 " {\n" + 211 " \"id\":50090220,\n" + 212 " \"parentId\":50090194,\n" + 213 " \"name\":\"运维前端研发组\",\n" + 214 " \"status\":1,\n" + 215 " \"fullName\":\"应用技术研发中心/前端研发部/运维前端研发组\"\n" + 216 " },\n" + 217 " {\n" + 218 " \"id\":50090221,\n" + 219 " \"parentId\":50090194,\n" + 220 " \"name\":\"华南基地前端研发组\",\n" + 221 " \"status\":1,\n" + 222 " \"fullName\":\"应用技术研发中心/前端研发部/华南基地前端研发组\"\n" + 223 " },\n" + 224 " {\n" + 225 " \"id\":50090222,\n" + 226 " \"parentId\":50090194,\n" + 227 " \"name\":\"补天产品前端研发部\",\n" + 228 " \"status\":1,\n" + 229 " \"fullName\":\"应用技术研发中心/前端研发部/补天产品前端研发部\"\n" + 230 " },\n" + 231 " {\n" + 232 " \"id\":50090789,\n" + 233 " \"parentId\":50090194,\n" + 234 " \"name\":\"前端一部\",\n" + 235 " \"status\":0,\n" + 236 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部\"\n" + 237 " },\n" + 238 " {\n" + 239 " \"id\":50090790,\n" + 240 " \"parentId\":50090789,\n" + 241 " \"name\":\"前端一组\",\n" + 242 " \"status\":0,\n" + 243 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/前端一组\"\n" + 244 " },\n" + 245 " {\n" + 246 " \"id\":50090795,\n" + 247 " \"parentId\":50090789,\n" + 248 " \"name\":\"前端三组\",\n" + 249 " \"status\":0,\n" + 250 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/前端三组\"\n" + 251 " },\n" + 252 " {\n" + 253 " \"id\":50090797,\n" + 254 " \"parentId\":50090789,\n" + 255 " \"name\":\"珠海组\",\n" + 256 " \"status\":0,\n" + 257 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/珠海组\"\n" + 258 " },\n" + 259 " {\n" + 260 " \"id\":50091815,\n" + 261 " \"parentId\":50090789,\n" + 262 " \"name\":\"西安组\",\n" + 263 " \"status\":0,\n" + 264 " \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/西安组\"\n" + 265 " },\n" + 266 " {\n" + 267 " \"id\":50090793,\n" + 268 " \"parentId\":50090194,\n" + 269 " \"name\":\"前端二部\",\n" + 270 " \"status\":0,\n" + 271 " \"fullName\":\"应用技术研发一中心/前端研发部/前端二部\"\n" + 272 " },\n" + 273 " {\n" + 274 " \"id\":50090791,\n" + 275 " \"parentId\":50090793,\n" + 276 " \"name\":\"前端四组\",\n" + 277 " \"status\":0,\n" + 278 " \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/前端四组\"\n" + 279 " },\n" + 280 " {\n" + 281 " \"id\":50090794,\n" + 282 " \"parentId\":50090793,\n" + 283 " \"name\":\"前端二组\",\n" + 284 " \"status\":0,\n" + 285 "\n" + 286 " \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/前端二组\"\n" + 287 " },\n" + 288 " {\n" + 289 " \"id\":50091816,\n" + 290 " \"parentId\":50090793,\n" + 291 " \"name\":\"上海组\",\n" + 292 " \"status\":0,\n" + 293 " \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/上海组\"\n" + 294 " },\n" + 295 " {\n" + 296 " \"id\":50090798,\n" + 297 " \"parentId\":50090194,\n" + 298 " \"name\":\"共性技术组\",\n" + 299 " \"status\":0,\n" + 300 " \"fullName\":\"应用技术研发一中心/前端研发部/共性技术组\"\n" + 301 " },\n" + 302 " {\n" + 303 " \"id\":50091814,\n" + 304 " \"parentId\":50090194,\n" + 305 " \"name\":\"前端三部\",\n" + 306 " \"status\":0,\n" + 307 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部\"\n" + 308 " },\n" + 309 " {\n" + 310 " \"id\":50090792,\n" + 311 " \"parentId\":50091814,\n" + 312 " \"name\":\"前端六组\",\n" + 313 " \"status\":0,\n" + 314 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端六组\"\n" + 315 " },\n" + 316 " {\n" + 317 " \"id\":50090796,\n" + 318 " \"parentId\":50091814,\n" + 319 " \"name\":\"前端七组\",\n" + 320 " \"status\":0,\n" + 321 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端七组\"\n" + 322 " },\n" + 323 " {\n" + 324 " \"id\":50091817,\n" + 325 " \"parentId\":50091814,\n" + 326 " \"name\":\"前端八组\",\n" + 327 " \"status\":0,\n" + 328 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端八组\"\n" + 329 " },\n" + 330 " {\n" + 331 " \"id\":50091818,\n" + 332 " \"parentId\":50091814,\n" + 333 " \"name\":\"前端九组\",\n" + 334 " \"status\":0,\n" + 335 " \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端九组\"\n" + 336 " },\n" + 337 " {\n" + 338 " \"id\":50090195,\n" + 339 " \"parentId\":50090193,\n" + 340 " \"name\":\"研发一部\",\n" + 341 " \"status\":0,\n" + 342 " \"fullName\":\"应用技术研发一中心/研发一部\"\n" + 343 " },\n" + 344 " {\n" + 345 " \"id\":50090620,\n" + 346 " \"parentId\":50090195,\n" + 347 " \"name\":\"策略管理组\",\n" + 348 " \"status\":0,\n" + 349 " \"fullName\":\"应用技术研发一中心/研发一部/策略管理组\"\n" + 350 " },\n" + 351 " {\n" + 352 " \"id\":50090621,\n" + 353 " \"parentId\":50090195,\n" + 354 " \"name\":\"集中管理组\",\n" + 355 " \"status\":0,\n" + 356 " \"fullName\":\"应用技术研发一中心/研发一部/集中管理组\"\n" + 357 " },\n" + 358 " {\n" + 359 " \"id\":50090622,\n" + 360 " \"parentId\":50090195,\n" + 361 " \"name\":\"产品规划与设计组\",\n" + 362 " \"status\":0,\n" + 363 " \"fullName\":\"应用技术研发一中心/研发一部/产品规划与设计组\"\n" + 364 " },\n" + 365 " {\n" + 366 " \"id\":50090632,\n" + 367 " \"parentId\":50090195,\n" + 368 " \"name\":\"研发管理组\",\n" + 369 " \"status\":0,\n" + 370 " \"fullName\":\"应用技术研发一中心/研发一部/研发管理组\"\n" + 371 " },\n" + 372 " {\n" + 373 " \"id\":50091022,\n" + 374 " \"parentId\":50090195,\n" + 375 " \"name\":\"解决方案组\",\n" + 376 " \"status\":0,\n" + 377 " \"fullName\":\"应用技术研发一中心/研发一部/解决方案组\"\n" + 378 " },\n" + 379 " {\n" + 380 " \"id\":50091023,\n" + 381 " \"parentId\":50090195,\n" + 382 " \"name\":\"公共服务组\",\n" + 383 " \"status\":0,\n" + 384 " \"fullName\":\"应用技术研发一中心/研发一部/公共服务组\"\n" + 385 "\n" + 386 " },\n" + 387 " {\n" + 388 " \"id\":50091024,\n" + 389 " \"parentId\":50090195,\n" + 390 " \"name\":\"网络拓普组\",\n" + 391 " \"status\":0,\n" + 392 " \"fullName\":\"应用技术研发一中心/研发一部/网络拓普组\"\n" + 393 " },\n" + 394 " {\n" + 395 " \"id\":50090197,\n" + 396 " \"parentId\":50090193,\n" + 397 " \"name\":\"研发三部\",\n" + 398 " \"status\":0,\n" + 399 " \"fullName\":\"应用技术研发一中心/研发三部\"\n" + 400 " },\n" + 401 " {\n" + 402 " \"id\":50090232,\n" + 403 " \"parentId\":50090197,\n" + 404 " \"name\":\"工业安全服务端研发组\",\n" + 405 " \"status\":0,\n" + 406 " \"fullName\":\"应用技术研发一中心/研发三部/工业安全服务端研发组\"\n" + 407 " },\n" + 408 " {\n" + 409 " \"id\":50090631,\n" + 410 " \"parentId\":50090193,\n" + 411 " \"name\":\"设计与研发支撑部\",\n" + 412 " \"status\":0,\n" + 413 " \"fullName\":\"应用技术研发一中心/设计与研发支撑部\"\n" + 414 " },\n" + 415 " {\n" + 416 " \"id\":50090633,\n" + 417 " \"parentId\":50090193,\n" + 418 " \"name\":\"UED\",\n" + 419 " \"status\":0,\n" + 420 " \"fullName\":\"应用技术研发一中心/UED\"\n" + 421 " },\n" + 422 " {\n" + 423 " \"id\":50090799,\n" + 424 " \"parentId\":50090193,\n" + 425 " \"name\":\"武汉研发部\",\n" + 426 " \"status\":0,\n" + 427 " \"fullName\":\"应用技术研发一中心/武汉研发部\"\n" + 428 " },\n" + 429 " {\n" + 430 " \"id\":50090800,\n" + 431 " \"parentId\":50090799,\n" + 432 " \"name\":\"武汉前端一组\",\n" + 433 " \"status\":0,\n" + 434 " \"fullName\":\"应用技术研发一中心/武汉研发部/武汉前端一组\"\n" + 435 " },\n" + 436 " {\n" + 437 " \"id\":50090801,\n" + 438 " \"parentId\":50090799,\n" + 439 " \"name\":\"武汉前端二组\",\n" + 440 " \"status\":0,\n" + 441 " \"fullName\":\"应用技术研发一中心/武汉研发部/武汉前端二组\"\n" + 442 " }]\n"; 443 //字符串转换为List 444 JSONArray jsonArray = JSONArray.parseArray(strJson); 445 List<Node> rootNode = (List<Node>)JSONArray.parseArray(strJson,Node.class); 446 // 查看结果 447 // for (Node node : rootNode) { 448 // System.out.println(node); 449 // } 450 // 最后的结果 451 List<Node> nodeList = new ArrayList<>(); 452 // 先找到所有的一级节点 453 for (int i = 0; i < rootNode.size(); i++) { 454 int index = 1; 455 for (int j = 0; j < rootNode.size();j++) { 456 if(rootNode.get(i).getParentId()==rootNode.get(j).getId()){ 457 index++; 458 } 459 } 460 //如果该ParentId无相等的id,则index仍然为1。将一级节点加入最后结果List中 461 if(index==1){ 462 nodeList.add(rootNode.get(i)); 463 } 464 } 465 // 为一级节点设置子节点,getChild是递归调用的 466 for (Node node : nodeList) { 467 node.setChildrenList(getChild(node.getId(), rootNode)); 468 } 469 Map<String,Object> jsonMap = new HashMap<>(); 470 jsonMap.put("node", nodeList); 471 System.out.println(JSONObject.toJSONString(jsonMap)); 472 } 473 474 private static List getChild(int id, List<Node> rootNode) { 475 // 子节点 476 List<Node> childList = new ArrayList<>(); 477 for (Node node : rootNode) { 478 // 遍历所有节点,如果该节点的父类id等于参数id,则作为孩子节点加入该节点中 479 if (node.getParentId()==id) { 480 childList.add(node); 481 } 482 483 } 484 // 把孩子节点的孩子节点循环一遍 485 for (Node node : childList) { 486 // 递归 487 node.setChildrenList(getChild(node.getId(), rootNode)); 488 } // 递归退出条件 489 if (childList.size() == 0) { 490 return null; 491 } 492 return childList; 493 } 494 495 }
1 package com.example.rabbitmq2; 2 3 import com.alibaba.fastjson.JSONObject; 4 import lombok.Data; 5 import lombok.Getter; 6 import lombok.Setter; 7 8 import java.util.ArrayList; 9 import java.util.Collections; 10 import java.util.Iterator; 11 import java.util.List; 12 13 @Data 14 public class Node { 15 /** 16 * 节点编号 17 */ 18 public int id; 19 /** 20 * 父节点编号 21 */ 22 public int parentId; 23 /** 24 * 节点名称 25 */ 26 public String name; 27 /** 28 * 节点状态 29 */ 30 public int status; 31 /** 32 * 节点全称 33 */ 34 public String fullName; 35 36 /** 37 * 孩子节点列表 38 */ 39 private List childrenList = new ArrayList(); 40 }
相关的博客: