zoukankan      html  css  js  c++  java
  • 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  ="[
    " +"" +
     18                 "    {
    " +
     19                 "        "id":50090113,
    " +
     20                 "        "parentId":50000001,
    " +
     21                 "        "name":"应用技术研发二中心",
    " +
     22                 "        "status":0,     
    " +
     23                 "        "fullName":"应用技术研发二中心"
    " +
     24                 "    },
    " +
     25                 "    {
    " +
     26                 "        "id":50090193,
    " +
     27                 "        "parentId":50000000,
    " +
     28                 "        "name":"应用技术研发一中心",
    " +
     29                 "        "status":0,     
    " +
     30                 "        "fullName":"应用技术研发一中心"
    " +
     31                 "    },
    " +
     32                 "    {
    " +
     33                 "        "id":50090194,
    " +
     34                 "        "parentId":50090193,
    " +
     35                 "        "name":"前端研发部",
    " +
     36                 "        "status":0,
    " +
     37                 "        "fullName":"应用技术研发一中心/前端研发部"
    " +
     38                 "    },
    " +
     39                 "    {
    " +
     40                 "        "id":50005413,
    " +
     41                 "        "parentId":50090194,
    " +
     42                 "        "name":"安全网关产品前端研发组",
    " +
     43                 "        "status":1,
    " +
     44                 "        "fullName":"应用技术研发中心/前端研发部/安全网关产品前端研发组"
    " +
     45                 "    },
    " +
     46                 "    {
    " +
     47                 "        "id":50005415,
    " +
     48                 "        "parentId":50090194,
    " +
     49                 "        "name":"前端研发二组",
    " +
     50                 "        "status":1,
    " +
     51                 "        "fullName":"应用技术研发中心/前端研发部/前端研发二组"
    " +
     52                 "    },
    " +
     53                 "    {
    " +
     54                 "        "id":50090199,
    " +
     55                 "        "parentId":50090194,
    " +
     56                 "        "name":"云安全前端研发组",
    " +
     57                 "        "status":1,
    " +
     58                 "        "fullName":"应用技术研发中心/前端研发部/云安全前端研发组"
    " +
     59                 "    },
    " +
     60                 "    {
    " +
     61                 "        "id":50090200,
    " +
     62                 "        "parentId":50090194,
    " +
     63                 "        "name":"威胁情报前端研发组",
    " +
     64                 "        "status":1,
    " +
     65                 "        "fullName":"应用技术研发中心/前端研发部/威胁情报前端研发组"
    " +
     66                 "    },
    " +
     67                 "    {
    " +
     68                 "        "id":50090201,
    " +
     69                 "        "parentId":50090194,
    " +
     70                 "        "name":"大数据与安全运营前端产品组",
    " +
     71                 "        "status":1,
    " +
     72                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端产品组"
    " +
     73                 "      
    " +
     74                 "    },
    " +
     75                 "    {
    " +
     76                 "        "id":50090202,
    " +
     77                 "        "parentId":50090194,
    " +
     78                 "        "name":"大数据与安全运营前端架构组",
    " +
     79                 "        "status":1,
    " +
     80                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端架构组"
    " +
     81                 "      
    " +
     82                 "    },
    " +
     83                 "    {
    " +
     84                 "        "id":50090203,
    " +
     85                 "        "parentId":50090194,
    " +
     86                 "        "name":"大数据与安全运营前端研发二组",
    " +
     87                 "        "status":1,
    " +
     88                 "        
    " +
     89                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发二组"
    " +
     90                 "    },
    " +
     91                 "    {
    " +
     92                 "        "id":50090204,
    " +
     93                 "        "parentId":50090194,
    " +
     94                 "        "name":"大数据与安全运营前端研发三组",
    " +
     95                 "        "status":1,
    " +
     96                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发三组"
    " +
     97                 "
    " +
     98                 "    },
    " +
     99                 "    {
    " +
    100                 "        "id":50090205,
    " +
    101                 "        "parentId":50090194,
    " +
    102                 "        "name":"大数据与安全运营前端研发四组",
    " +
    103                 "        "status":1,
    " +
    104                 "       
    " +
    105                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发四组"
    " +
    106                 "
    " +
    107                 "    },
    " +
    108                 "    {
    " +
    109                 "        "id":50090206,
    " +
    110                 "        "parentId":50090194,
    " +
    111                 "        "name":"大数据与安全运营前端研发运营商部",
    " +
    112                 "        "status":1,
    " +
    113                 "        
    " +
    114                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发运营商部"
    " +
    115                 "
    " +
    116                 "    },
    " +
    117                 "    {
    " +
    118                 "        "id":50090207,
    " +
    119                 "        "parentId":50090194,
    " +
    120                 "        "name":"大数据与安全运营前端研发政企部",
    " +
    121                 "        "status":1,      
    " +
    122                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发政企部"
    " +
    123                 "    },
    " +
    124                 "    {
    " +
    125                 "        "id":50090208,
    " +
    126                 "        "parentId":50090194,
    " +
    127                 "        "name":"态势感知前端研发组",
    " +
    128                 "        "status":1,
    " +
    129                 "        "fullName":"应用技术研发中心/前端研发部/态势感知前端研发组"
    " +
    130                 "    },
    " +
    131                 "    {
    " +
    132                 "        "id":50090209,
    " +
    133                 "        "parentId":50090194,
    " +
    134                 "        "name":"大数据应用前端研发组",
    " +
    135                 "        "status":1,
    " +
    136                 "        "fullName":"应用技术研发中心/前端研发部/大数据应用前端研发组"
    " +
    137                 "    },
    " +
    138                 "    {
    " +
    139                 "        "id":50090210,
    " +
    140                 "        "parentId":50090194,
    " +
    141                 "        "name":"大数据与威胁分析前端研发一组",
    " +
    142                 "        "status":1,
    " +
    143                 "        "fullName":"应用技术研发中心/前端研发部/大数据与威胁分析前端研发一组"
    " +
    144                 "    },
    " +
    145                 "    {
    " +
    146                 "        "id":50090211,
    " +
    147                 "        "parentId":50090194,
    " +
    148                 "        "name":"大数据与威胁公司前端研发二组",
    " +
    149                 "        "status":1,
    " +
    150                 "        "fullName":"应用技术研发中心/前端研发部/大数据与威胁公司前端研发二组"
    " +
    151                 "    },
    " +
    152                 "    {
    " +
    153                 "        "id":50090212,
    " +
    154                 "        "parentId":50090194,
    " +
    155                 "        "name":"大数据与威胁分析前端研发三组",
    " +
    156                 "        "status":1,
    " +
    157                 "        "fullName":"应用技术研发中心/前端研发部/大数据与威胁分析前端研发三组"
    " +
    158                 "    },
    " +
    159                 "    {
    " +
    160                 "        "id":50090213,
    " +
    161                 "        "parentId":50090194,
    " +
    162                 "        "name":"工业安全前端研发组",
    " +
    163                 "        "status":1,
    " +
    164                 "        "fullName":"应用技术研发中心/前端研发部/工业安全前端研发组"
    " +
    165                 "    },
    " +
    166                 "    {
    " +
    167                 "        "id":50090214,
    " +
    168                 "        "parentId":50090194,
    " +
    169                 "        "name":"行为安全前端研发组",
    " +
    170                 "        "status":1,
    " +
    171                 "        "fullName":"应用技术研发中心/前端研发部/行为安全前端研发组"
    " +
    172                 "    },
    " +
    173                 "    {
    " +
    174                 "        "id":50090215,
    " +
    175                 "        "parentId":50090194,
    " +
    176                 "        "name":"基础大数据前端研发组",
    " +
    177                 "        "status":1,
    " +
    178                 "        "fullName":"应用技术研发中心/前端研发部/基础大数据前端研发组"
    " +
    179                 "    },
    " +
    180                 "    {
    " +
    181                 "        "id":50090216,
    " +
    182                 "        "parentId":50090194,
    " +
    183                 "        "name":"应用产品前端研发组",
    " +
    184                 "        "status":1,
    " +
    185                 "        "fullName":"应用技术研发中心/前端研发部/应用产品前端研发组"
    " +
    186                 "    },
    " +
    187                 "    {
    " +
    188                 "        "id":50090217,
    " +
    189                 "        "parentId":50090194,
    " +
    190                 "        "name":"终端安全产品前端研发组",
    " +
    191                 "        "status":0,
    " +
    192                 "        "fullName":"应用技术研发一中心/前端研发部/终端安全产品前端研发组"
    " +
    193                 "    },
    " +
    194                 "    {
    " +
    195                 "        "id":50090218,
    " +
    196                 "        "parentId":50090194,
    " +
    197                 "        "name":"安全产品前端研发组",
    " +
    198                 "        "status":1,
    " +
    199                 "        "fullName":"应用技术研发中心/前端研发部/安全产品前端研发组"
    " +
    200                 "    },
    " +
    201                 "    {
    " +
    202                 "        "id":50090219,
    " +
    203                 "        "parentId":50090194,
    " +
    204                 "        "name":"数据安全前端研发组",
    " +
    205                 "        "status":0,
    " +
    206                 "        "fullName":"应用技术研发一中心/前端研发部/数据安全前端研发组"
    " +
    207                 "    },
    " +
    208                 "    {
    " +
    209                 "        "id":50090220,
    " +
    210                 "        "parentId":50090194,
    " +
    211                 "        "name":"运维前端研发组",
    " +
    212                 "        "status":1,
    " +
    213                 "        "fullName":"应用技术研发中心/前端研发部/运维前端研发组"
    " +
    214                 "    },
    " +
    215                 "    {
    " +
    216                 "        "id":50090221,
    " +
    217                 "        "parentId":50090194,
    " +
    218                 "        "name":"华南基地前端研发组",
    " +
    219                 "        "status":1,
    " +
    220                 "        "fullName":"应用技术研发中心/前端研发部/华南基地前端研发组"
    " +
    221                 "    },
    " +
    222                 "    {
    " +
    223                 "        "id":50090222,
    " +
    224                 "        "parentId":50090194,
    " +
    225                 "        "name":"补天产品前端研发部",
    " +
    226                 "        "status":1,
    " +
    227                 "        "fullName":"应用技术研发中心/前端研发部/补天产品前端研发部"
    " +
    228                 "    },
    " +
    229                 "    {
    " +
    230                 "        "id":50090789,
    " +
    231                 "        "parentId":50090194,
    " +
    232                 "        "name":"前端一部",
    " +
    233                 "        "status":0,
    " +
    234                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部"
    " +
    235                 "    },
    " +
    236                 "    {
    " +
    237                 "        "id":50090790,
    " +
    238                 "        "parentId":50090789,
    " +
    239                 "        "name":"前端一组",
    " +
    240                 "        "status":0,
    " +
    241                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部/前端一组"
    " +
    242                 "    },
    " +
    243                 "    {
    " +
    244                 "        "id":50090795,
    " +
    245                 "        "parentId":50090789,
    " +
    246                 "        "name":"前端三组",
    " +
    247                 "        "status":0,
    " +
    248                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部/前端三组"
    " +
    249                 "    },
    " +
    250                 "    {
    " +
    251                 "        "id":50090797,
    " +
    252                 "        "parentId":50090789,
    " +
    253                 "        "name":"珠海组",
    " +
    254                 "        "status":0,
    " +
    255                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部/珠海组"
    " +
    256                 "    },
    " +
    257                 "    {
    " +
    258                 "        "id":50091815,
    " +
    259                 "        "parentId":50090789,
    " +
    260                 "        "name":"西安组",
    " +
    261                 "        "status":0,
    " +
    262                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部/西安组"
    " +
    263                 "    },
    " +
    264                 "    {
    " +
    265                 "        "id":50090793,
    " +
    266                 "        "parentId":50090194,
    " +
    267                 "        "name":"前端二部",
    " +
    268                 "        "status":0,
    " +
    269                 "        "fullName":"应用技术研发一中心/前端研发部/前端二部"
    " +
    270                 "    },
    " +
    271                 "    {
    " +
    272                 "        "id":50090791,
    " +
    273                 "        "parentId":50090793,
    " +
    274                 "        "name":"前端四组",
    " +
    275                 "        "status":0,
    " +
    276                 "        "fullName":"应用技术研发一中心/前端研发部/前端二部/前端四组"
    " +
    277                 "    },
    " +
    278                 "    {
    " +
    279                 "        "id":50090794,
    " +
    280                 "        "parentId":50090793,
    " +
    281                 "        "name":"前端二组",
    " +
    282                 "        "status":0,
    " +
    283                 "
    " +
    284                 "        "fullName":"应用技术研发一中心/前端研发部/前端二部/前端二组"
    " +
    285                 "    },
    " +
    286                 "    {
    " +
    287                 "        "id":50091816,
    " +
    288                 "        "parentId":50090793,
    " +
    289                 "        "name":"上海组",
    " +
    290                 "        "status":0,
    " +
    291                 "        "fullName":"应用技术研发一中心/前端研发部/前端二部/上海组"
    " +
    292                 "    },
    " +
    293                 "    {
    " +
    294                 "        "id":50090798,
    " +
    295                 "        "parentId":50090194,
    " +
    296                 "        "name":"共性技术组",
    " +
    297                 "        "status":0,
    " +
    298                 "        "fullName":"应用技术研发一中心/前端研发部/共性技术组"
    " +
    299                 "    },
    " +
    300                 "    {
    " +
    301                 "        "id":50091814,
    " +
    302                 "        "parentId":50090194,
    " +
    303                 "        "name":"前端三部",
    " +
    304                 "        "status":0,
    " +
    305                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部"
    " +
    306                 "    },
    " +
    307                 "    {
    " +
    308                 "        "id":50090792,
    " +
    309                 "        "parentId":50091814,
    " +
    310                 "        "name":"前端六组",
    " +
    311                 "        "status":0,
    " +
    312                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部/前端六组"
    " +
    313                 "    },
    " +
    314                 "    {
    " +
    315                 "        "id":50090796,
    " +
    316                 "        "parentId":50091814,
    " +
    317                 "        "name":"前端七组",
    " +
    318                 "        "status":0,
    " +
    319                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部/前端七组"
    " +
    320                 "    },
    " +
    321                 "    {
    " +
    322                 "        "id":50091817,
    " +
    323                 "        "parentId":50091814,
    " +
    324                 "        "name":"前端八组",
    " +
    325                 "        "status":0,
    " +
    326                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部/前端八组"
    " +
    327                 "    },
    " +
    328                 "    {
    " +
    329                 "        "id":50091818,
    " +
    330                 "        "parentId":50091814,
    " +
    331                 "        "name":"前端九组",
    " +
    332                 "        "status":0,
    " +
    333                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部/前端九组"
    " +
    334                 "    },
    " +
    335                 "    {
    " +
    336                 "        "id":50090195,
    " +
    337                 "        "parentId":50090193,
    " +
    338                 "        "name":"研发一部",
    " +
    339                 "        "status":0,
    " +
    340                 "        "fullName":"应用技术研发一中心/研发一部"
    " +
    341                 "    },
    " +
    342                 "    {
    " +
    343                 "        "id":50090620,
    " +
    344                 "        "parentId":50090195,
    " +
    345                 "        "name":"策略管理组",
    " +
    346                 "        "status":0,
    " +
    347                 "        "fullName":"应用技术研发一中心/研发一部/策略管理组"
    " +
    348                 "    },
    " +
    349                 "    {
    " +
    350                 "        "id":50090621,
    " +
    351                 "        "parentId":50090195,
    " +
    352                 "        "name":"集中管理组",
    " +
    353                 "        "status":0,
    " +
    354                 "        "fullName":"应用技术研发一中心/研发一部/集中管理组"
    " +
    355                 "    },
    " +
    356                 "    {
    " +
    357                 "        "id":50090622,
    " +
    358                 "        "parentId":50090195,
    " +
    359                 "        "name":"产品规划与设计组",
    " +
    360                 "        "status":0,
    " +
    361                 "        "fullName":"应用技术研发一中心/研发一部/产品规划与设计组"
    " +
    362                 "    },
    " +
    363                 "    {
    " +
    364                 "        "id":50090632,
    " +
    365                 "        "parentId":50090195,
    " +
    366                 "        "name":"研发管理组",
    " +
    367                 "        "status":0,
    " +
    368                 "        "fullName":"应用技术研发一中心/研发一部/研发管理组"
    " +
    369                 "    },
    " +
    370                 "    {
    " +
    371                 "        "id":50091022,
    " +
    372                 "        "parentId":50090195,
    " +
    373                 "        "name":"解决方案组",
    " +
    374                 "        "status":0,
    " +
    375                 "        "fullName":"应用技术研发一中心/研发一部/解决方案组"
    " +
    376                 "    },
    " +
    377                 "    {
    " +
    378                 "        "id":50091023,
    " +
    379                 "        "parentId":50090195,
    " +
    380                 "        "name":"公共服务组",
    " +
    381                 "        "status":0,
    " +
    382                 "        "fullName":"应用技术研发一中心/研发一部/公共服务组"
    " +
    383                 "
    " +
    384                 "    },
    " +
    385                 "    {
    " +
    386                 "        "id":50091024,
    " +
    387                 "        "parentId":50090195,
    " +
    388                 "        "name":"网络拓普组",
    " +
    389                 "        "status":0,
    " +
    390                 "        "fullName":"应用技术研发一中心/研发一部/网络拓普组"
    " +
    391                 "    },
    " +
    392                 "    {
    " +
    393                 "        "id":50090197,
    " +
    394                 "        "parentId":50090193,
    " +
    395                 "        "name":"研发三部",
    " +
    396                 "        "status":0,
    " +
    397                 "        "fullName":"应用技术研发一中心/研发三部"
    " +
    398                 "    },
    " +
    399                 "    {
    " +
    400                 "        "id":50090232,
    " +
    401                 "        "parentId":50090197,
    " +
    402                 "        "name":"工业安全服务端研发组",
    " +
    403                 "        "status":0,
    " +
    404                 "        "fullName":"应用技术研发一中心/研发三部/工业安全服务端研发组"
    " +
    405                 "    },
    " +
    406                 "    {
    " +
    407                 "        "id":50090631,
    " +
    408                 "        "parentId":50090193,
    " +
    409                 "        "name":"设计与研发支撑部",
    " +
    410                 "        "status":0,
    " +
    411                 "        "fullName":"应用技术研发一中心/设计与研发支撑部"
    " +
    412                 "    },
    " +
    413                 "    {
    " +
    414                 "        "id":50090633,
    " +
    415                 "        "parentId":50090193,
    " +
    416                 "        "name":"UED",
    " +
    417                 "        "status":0,
    " +
    418                 "        "fullName":"应用技术研发一中心/UED"
    " +
    419                 "    },
    " +
    420                 "    {
    " +
    421                 "        "id":50090799,
    " +
    422                 "        "parentId":50090193,
    " +
    423                 "        "name":"武汉研发部",
    " +
    424                 "        "status":0,
    " +
    425                 "        "fullName":"应用技术研发一中心/武汉研发部"
    " +
    426                 "    },
    " +
    427                 "    {
    " +
    428                 "        "id":50090800,
    " +
    429                 "        "parentId":50090799,
    " +
    430                 "        "name":"武汉前端一组",
    " +
    431                 "        "status":0,
    " +
    432                 "        "fullName":"应用技术研发一中心/武汉研发部/武汉前端一组"
    " +
    433                 "    },
    " +
    434                 "    {
    " +
    435                 "        "id":50090801,
    " +
    436                 "        "parentId":50090799,
    " +
    437                 "        "name":"武汉前端二组",
    " +
    438                 "        "status":0,
    " +
    439                 "        "fullName":"应用技术研发一中心/武汉研发部/武汉前端二组"
    " +
    440                 "    }]
    ";
    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  ="[
    " +"" +
     20                 "    {
    " +
     21                 "        "id":50090113,
    " +
     22                 "        "parentId":50000001,
    " +
     23                 "        "name":"应用技术研发二中心",
    " +
     24                 "        "status":0,     
    " +
     25                 "        "fullName":"应用技术研发二中心"
    " +
     26                 "    },
    " +
     27                 "    {
    " +
     28                 "        "id":50090193,
    " +
     29                 "        "parentId":50000000,
    " +
     30                 "        "name":"应用技术研发一中心",
    " +
     31                 "        "status":0,     
    " +
     32                 "        "fullName":"应用技术研发一中心"
    " +
     33                 "    },
    " +
     34                 "    {
    " +
     35                 "        "id":50090194,
    " +
     36                 "        "parentId":50090193,
    " +
     37                 "        "name":"前端研发部",
    " +
     38                 "        "status":0,
    " +
     39                 "        "fullName":"应用技术研发一中心/前端研发部"
    " +
     40                 "    },
    " +
     41                 "    {
    " +
     42                 "        "id":50005413,
    " +
     43                 "        "parentId":50090194,
    " +
     44                 "        "name":"安全网关产品前端研发组",
    " +
     45                 "        "status":1,
    " +
     46                 "        "fullName":"应用技术研发中心/前端研发部/安全网关产品前端研发组"
    " +
     47                 "    },
    " +
     48                 "    {
    " +
     49                 "        "id":50005415,
    " +
     50                 "        "parentId":50090194,
    " +
     51                 "        "name":"前端研发二组",
    " +
     52                 "        "status":1,
    " +
     53                 "        "fullName":"应用技术研发中心/前端研发部/前端研发二组"
    " +
     54                 "    },
    " +
     55                 "    {
    " +
     56                 "        "id":50090199,
    " +
     57                 "        "parentId":50090194,
    " +
     58                 "        "name":"云安全前端研发组",
    " +
     59                 "        "status":1,
    " +
     60                 "        "fullName":"应用技术研发中心/前端研发部/云安全前端研发组"
    " +
     61                 "    },
    " +
     62                 "    {
    " +
     63                 "        "id":50090200,
    " +
     64                 "        "parentId":50090194,
    " +
     65                 "        "name":"威胁情报前端研发组",
    " +
     66                 "        "status":1,
    " +
     67                 "        "fullName":"应用技术研发中心/前端研发部/威胁情报前端研发组"
    " +
     68                 "    },
    " +
     69                 "    {
    " +
     70                 "        "id":50090201,
    " +
     71                 "        "parentId":50090194,
    " +
     72                 "        "name":"大数据与安全运营前端产品组",
    " +
     73                 "        "status":1,
    " +
     74                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端产品组"
    " +
     75                 "      
    " +
     76                 "    },
    " +
     77                 "    {
    " +
     78                 "        "id":50090202,
    " +
     79                 "        "parentId":50090194,
    " +
     80                 "        "name":"大数据与安全运营前端架构组",
    " +
     81                 "        "status":1,
    " +
     82                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端架构组"
    " +
     83                 "      
    " +
     84                 "    },
    " +
     85                 "    {
    " +
     86                 "        "id":50090203,
    " +
     87                 "        "parentId":50090194,
    " +
     88                 "        "name":"大数据与安全运营前端研发二组",
    " +
     89                 "        "status":1,
    " +
     90                 "        
    " +
     91                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发二组"
    " +
     92                 "    },
    " +
     93                 "    {
    " +
     94                 "        "id":50090204,
    " +
     95                 "        "parentId":50090194,
    " +
     96                 "        "name":"大数据与安全运营前端研发三组",
    " +
     97                 "        "status":1,
    " +
     98                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发三组"
    " +
     99                 "
    " +
    100                 "    },
    " +
    101                 "    {
    " +
    102                 "        "id":50090205,
    " +
    103                 "        "parentId":50090194,
    " +
    104                 "        "name":"大数据与安全运营前端研发四组",
    " +
    105                 "        "status":1,
    " +
    106                 "       
    " +
    107                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发四组"
    " +
    108                 "
    " +
    109                 "    },
    " +
    110                 "    {
    " +
    111                 "        "id":50090206,
    " +
    112                 "        "parentId":50090194,
    " +
    113                 "        "name":"大数据与安全运营前端研发运营商部",
    " +
    114                 "        "status":1,
    " +
    115                 "        
    " +
    116                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发运营商部"
    " +
    117                 "
    " +
    118                 "    },
    " +
    119                 "    {
    " +
    120                 "        "id":50090207,
    " +
    121                 "        "parentId":50090194,
    " +
    122                 "        "name":"大数据与安全运营前端研发政企部",
    " +
    123                 "        "status":1,      
    " +
    124                 "        "fullName":"应用技术研发中心/前端研发部/大数据与安全运营前端研发政企部"
    " +
    125                 "    },
    " +
    126                 "    {
    " +
    127                 "        "id":50090208,
    " +
    128                 "        "parentId":50090194,
    " +
    129                 "        "name":"态势感知前端研发组",
    " +
    130                 "        "status":1,
    " +
    131                 "        "fullName":"应用技术研发中心/前端研发部/态势感知前端研发组"
    " +
    132                 "    },
    " +
    133                 "    {
    " +
    134                 "        "id":50090209,
    " +
    135                 "        "parentId":50090194,
    " +
    136                 "        "name":"大数据应用前端研发组",
    " +
    137                 "        "status":1,
    " +
    138                 "        "fullName":"应用技术研发中心/前端研发部/大数据应用前端研发组"
    " +
    139                 "    },
    " +
    140                 "    {
    " +
    141                 "        "id":50090210,
    " +
    142                 "        "parentId":50090194,
    " +
    143                 "        "name":"大数据与威胁分析前端研发一组",
    " +
    144                 "        "status":1,
    " +
    145                 "        "fullName":"应用技术研发中心/前端研发部/大数据与威胁分析前端研发一组"
    " +
    146                 "    },
    " +
    147                 "    {
    " +
    148                 "        "id":50090211,
    " +
    149                 "        "parentId":50090194,
    " +
    150                 "        "name":"大数据与威胁公司前端研发二组",
    " +
    151                 "        "status":1,
    " +
    152                 "        "fullName":"应用技术研发中心/前端研发部/大数据与威胁公司前端研发二组"
    " +
    153                 "    },
    " +
    154                 "    {
    " +
    155                 "        "id":50090212,
    " +
    156                 "        "parentId":50090194,
    " +
    157                 "        "name":"大数据与威胁分析前端研发三组",
    " +
    158                 "        "status":1,
    " +
    159                 "        "fullName":"应用技术研发中心/前端研发部/大数据与威胁分析前端研发三组"
    " +
    160                 "    },
    " +
    161                 "    {
    " +
    162                 "        "id":50090213,
    " +
    163                 "        "parentId":50090194,
    " +
    164                 "        "name":"工业安全前端研发组",
    " +
    165                 "        "status":1,
    " +
    166                 "        "fullName":"应用技术研发中心/前端研发部/工业安全前端研发组"
    " +
    167                 "    },
    " +
    168                 "    {
    " +
    169                 "        "id":50090214,
    " +
    170                 "        "parentId":50090194,
    " +
    171                 "        "name":"行为安全前端研发组",
    " +
    172                 "        "status":1,
    " +
    173                 "        "fullName":"应用技术研发中心/前端研发部/行为安全前端研发组"
    " +
    174                 "    },
    " +
    175                 "    {
    " +
    176                 "        "id":50090215,
    " +
    177                 "        "parentId":50090194,
    " +
    178                 "        "name":"基础大数据前端研发组",
    " +
    179                 "        "status":1,
    " +
    180                 "        "fullName":"应用技术研发中心/前端研发部/基础大数据前端研发组"
    " +
    181                 "    },
    " +
    182                 "    {
    " +
    183                 "        "id":50090216,
    " +
    184                 "        "parentId":50090194,
    " +
    185                 "        "name":"应用产品前端研发组",
    " +
    186                 "        "status":1,
    " +
    187                 "        "fullName":"应用技术研发中心/前端研发部/应用产品前端研发组"
    " +
    188                 "    },
    " +
    189                 "    {
    " +
    190                 "        "id":50090217,
    " +
    191                 "        "parentId":50090194,
    " +
    192                 "        "name":"终端安全产品前端研发组",
    " +
    193                 "        "status":0,
    " +
    194                 "        "fullName":"应用技术研发一中心/前端研发部/终端安全产品前端研发组"
    " +
    195                 "    },
    " +
    196                 "    {
    " +
    197                 "        "id":50090218,
    " +
    198                 "        "parentId":50090194,
    " +
    199                 "        "name":"安全产品前端研发组",
    " +
    200                 "        "status":1,
    " +
    201                 "        "fullName":"应用技术研发中心/前端研发部/安全产品前端研发组"
    " +
    202                 "    },
    " +
    203                 "    {
    " +
    204                 "        "id":50090219,
    " +
    205                 "        "parentId":50090194,
    " +
    206                 "        "name":"数据安全前端研发组",
    " +
    207                 "        "status":0,
    " +
    208                 "        "fullName":"应用技术研发一中心/前端研发部/数据安全前端研发组"
    " +
    209                 "    },
    " +
    210                 "    {
    " +
    211                 "        "id":50090220,
    " +
    212                 "        "parentId":50090194,
    " +
    213                 "        "name":"运维前端研发组",
    " +
    214                 "        "status":1,
    " +
    215                 "        "fullName":"应用技术研发中心/前端研发部/运维前端研发组"
    " +
    216                 "    },
    " +
    217                 "    {
    " +
    218                 "        "id":50090221,
    " +
    219                 "        "parentId":50090194,
    " +
    220                 "        "name":"华南基地前端研发组",
    " +
    221                 "        "status":1,
    " +
    222                 "        "fullName":"应用技术研发中心/前端研发部/华南基地前端研发组"
    " +
    223                 "    },
    " +
    224                 "    {
    " +
    225                 "        "id":50090222,
    " +
    226                 "        "parentId":50090194,
    " +
    227                 "        "name":"补天产品前端研发部",
    " +
    228                 "        "status":1,
    " +
    229                 "        "fullName":"应用技术研发中心/前端研发部/补天产品前端研发部"
    " +
    230                 "    },
    " +
    231                 "    {
    " +
    232                 "        "id":50090789,
    " +
    233                 "        "parentId":50090194,
    " +
    234                 "        "name":"前端一部",
    " +
    235                 "        "status":0,
    " +
    236                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部"
    " +
    237                 "    },
    " +
    238                 "    {
    " +
    239                 "        "id":50090790,
    " +
    240                 "        "parentId":50090789,
    " +
    241                 "        "name":"前端一组",
    " +
    242                 "        "status":0,
    " +
    243                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部/前端一组"
    " +
    244                 "    },
    " +
    245                 "    {
    " +
    246                 "        "id":50090795,
    " +
    247                 "        "parentId":50090789,
    " +
    248                 "        "name":"前端三组",
    " +
    249                 "        "status":0,
    " +
    250                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部/前端三组"
    " +
    251                 "    },
    " +
    252                 "    {
    " +
    253                 "        "id":50090797,
    " +
    254                 "        "parentId":50090789,
    " +
    255                 "        "name":"珠海组",
    " +
    256                 "        "status":0,
    " +
    257                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部/珠海组"
    " +
    258                 "    },
    " +
    259                 "    {
    " +
    260                 "        "id":50091815,
    " +
    261                 "        "parentId":50090789,
    " +
    262                 "        "name":"西安组",
    " +
    263                 "        "status":0,
    " +
    264                 "        "fullName":"应用技术研发一中心/前端研发部/前端一部/西安组"
    " +
    265                 "    },
    " +
    266                 "    {
    " +
    267                 "        "id":50090793,
    " +
    268                 "        "parentId":50090194,
    " +
    269                 "        "name":"前端二部",
    " +
    270                 "        "status":0,
    " +
    271                 "        "fullName":"应用技术研发一中心/前端研发部/前端二部"
    " +
    272                 "    },
    " +
    273                 "    {
    " +
    274                 "        "id":50090791,
    " +
    275                 "        "parentId":50090793,
    " +
    276                 "        "name":"前端四组",
    " +
    277                 "        "status":0,
    " +
    278                 "        "fullName":"应用技术研发一中心/前端研发部/前端二部/前端四组"
    " +
    279                 "    },
    " +
    280                 "    {
    " +
    281                 "        "id":50090794,
    " +
    282                 "        "parentId":50090793,
    " +
    283                 "        "name":"前端二组",
    " +
    284                 "        "status":0,
    " +
    285                 "
    " +
    286                 "        "fullName":"应用技术研发一中心/前端研发部/前端二部/前端二组"
    " +
    287                 "    },
    " +
    288                 "    {
    " +
    289                 "        "id":50091816,
    " +
    290                 "        "parentId":50090793,
    " +
    291                 "        "name":"上海组",
    " +
    292                 "        "status":0,
    " +
    293                 "        "fullName":"应用技术研发一中心/前端研发部/前端二部/上海组"
    " +
    294                 "    },
    " +
    295                 "    {
    " +
    296                 "        "id":50090798,
    " +
    297                 "        "parentId":50090194,
    " +
    298                 "        "name":"共性技术组",
    " +
    299                 "        "status":0,
    " +
    300                 "        "fullName":"应用技术研发一中心/前端研发部/共性技术组"
    " +
    301                 "    },
    " +
    302                 "    {
    " +
    303                 "        "id":50091814,
    " +
    304                 "        "parentId":50090194,
    " +
    305                 "        "name":"前端三部",
    " +
    306                 "        "status":0,
    " +
    307                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部"
    " +
    308                 "    },
    " +
    309                 "    {
    " +
    310                 "        "id":50090792,
    " +
    311                 "        "parentId":50091814,
    " +
    312                 "        "name":"前端六组",
    " +
    313                 "        "status":0,
    " +
    314                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部/前端六组"
    " +
    315                 "    },
    " +
    316                 "    {
    " +
    317                 "        "id":50090796,
    " +
    318                 "        "parentId":50091814,
    " +
    319                 "        "name":"前端七组",
    " +
    320                 "        "status":0,
    " +
    321                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部/前端七组"
    " +
    322                 "    },
    " +
    323                 "    {
    " +
    324                 "        "id":50091817,
    " +
    325                 "        "parentId":50091814,
    " +
    326                 "        "name":"前端八组",
    " +
    327                 "        "status":0,
    " +
    328                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部/前端八组"
    " +
    329                 "    },
    " +
    330                 "    {
    " +
    331                 "        "id":50091818,
    " +
    332                 "        "parentId":50091814,
    " +
    333                 "        "name":"前端九组",
    " +
    334                 "        "status":0,
    " +
    335                 "        "fullName":"应用技术研发一中心/前端研发部/前端三部/前端九组"
    " +
    336                 "    },
    " +
    337                 "    {
    " +
    338                 "        "id":50090195,
    " +
    339                 "        "parentId":50090193,
    " +
    340                 "        "name":"研发一部",
    " +
    341                 "        "status":0,
    " +
    342                 "        "fullName":"应用技术研发一中心/研发一部"
    " +
    343                 "    },
    " +
    344                 "    {
    " +
    345                 "        "id":50090620,
    " +
    346                 "        "parentId":50090195,
    " +
    347                 "        "name":"策略管理组",
    " +
    348                 "        "status":0,
    " +
    349                 "        "fullName":"应用技术研发一中心/研发一部/策略管理组"
    " +
    350                 "    },
    " +
    351                 "    {
    " +
    352                 "        "id":50090621,
    " +
    353                 "        "parentId":50090195,
    " +
    354                 "        "name":"集中管理组",
    " +
    355                 "        "status":0,
    " +
    356                 "        "fullName":"应用技术研发一中心/研发一部/集中管理组"
    " +
    357                 "    },
    " +
    358                 "    {
    " +
    359                 "        "id":50090622,
    " +
    360                 "        "parentId":50090195,
    " +
    361                 "        "name":"产品规划与设计组",
    " +
    362                 "        "status":0,
    " +
    363                 "        "fullName":"应用技术研发一中心/研发一部/产品规划与设计组"
    " +
    364                 "    },
    " +
    365                 "    {
    " +
    366                 "        "id":50090632,
    " +
    367                 "        "parentId":50090195,
    " +
    368                 "        "name":"研发管理组",
    " +
    369                 "        "status":0,
    " +
    370                 "        "fullName":"应用技术研发一中心/研发一部/研发管理组"
    " +
    371                 "    },
    " +
    372                 "    {
    " +
    373                 "        "id":50091022,
    " +
    374                 "        "parentId":50090195,
    " +
    375                 "        "name":"解决方案组",
    " +
    376                 "        "status":0,
    " +
    377                 "        "fullName":"应用技术研发一中心/研发一部/解决方案组"
    " +
    378                 "    },
    " +
    379                 "    {
    " +
    380                 "        "id":50091023,
    " +
    381                 "        "parentId":50090195,
    " +
    382                 "        "name":"公共服务组",
    " +
    383                 "        "status":0,
    " +
    384                 "        "fullName":"应用技术研发一中心/研发一部/公共服务组"
    " +
    385                 "
    " +
    386                 "    },
    " +
    387                 "    {
    " +
    388                 "        "id":50091024,
    " +
    389                 "        "parentId":50090195,
    " +
    390                 "        "name":"网络拓普组",
    " +
    391                 "        "status":0,
    " +
    392                 "        "fullName":"应用技术研发一中心/研发一部/网络拓普组"
    " +
    393                 "    },
    " +
    394                 "    {
    " +
    395                 "        "id":50090197,
    " +
    396                 "        "parentId":50090193,
    " +
    397                 "        "name":"研发三部",
    " +
    398                 "        "status":0,
    " +
    399                 "        "fullName":"应用技术研发一中心/研发三部"
    " +
    400                 "    },
    " +
    401                 "    {
    " +
    402                 "        "id":50090232,
    " +
    403                 "        "parentId":50090197,
    " +
    404                 "        "name":"工业安全服务端研发组",
    " +
    405                 "        "status":0,
    " +
    406                 "        "fullName":"应用技术研发一中心/研发三部/工业安全服务端研发组"
    " +
    407                 "    },
    " +
    408                 "    {
    " +
    409                 "        "id":50090631,
    " +
    410                 "        "parentId":50090193,
    " +
    411                 "        "name":"设计与研发支撑部",
    " +
    412                 "        "status":0,
    " +
    413                 "        "fullName":"应用技术研发一中心/设计与研发支撑部"
    " +
    414                 "    },
    " +
    415                 "    {
    " +
    416                 "        "id":50090633,
    " +
    417                 "        "parentId":50090193,
    " +
    418                 "        "name":"UED",
    " +
    419                 "        "status":0,
    " +
    420                 "        "fullName":"应用技术研发一中心/UED"
    " +
    421                 "    },
    " +
    422                 "    {
    " +
    423                 "        "id":50090799,
    " +
    424                 "        "parentId":50090193,
    " +
    425                 "        "name":"武汉研发部",
    " +
    426                 "        "status":0,
    " +
    427                 "        "fullName":"应用技术研发一中心/武汉研发部"
    " +
    428                 "    },
    " +
    429                 "    {
    " +
    430                 "        "id":50090800,
    " +
    431                 "        "parentId":50090799,
    " +
    432                 "        "name":"武汉前端一组",
    " +
    433                 "        "status":0,
    " +
    434                 "        "fullName":"应用技术研发一中心/武汉研发部/武汉前端一组"
    " +
    435                 "    },
    " +
    436                 "    {
    " +
    437                 "        "id":50090801,
    " +
    438                 "        "parentId":50090799,
    " +
    439                 "        "name":"武汉前端二组",
    " +
    440                 "        "status":0,
    " +
    441                 "        "fullName":"应用技术研发一中心/武汉研发部/武汉前端二组"
    " +
    442                 "    }]
    ";
    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 }

     相关的博客:

     1.java、js中实现无限层级的树形结构(类似递归)

     2.多级菜单递归遍历成树

  • 相关阅读:
    详解实现Android中实现View滑动的几种方式
    一起写一个Android图片轮播控件
    Java核心技术点之多线程
    深入了解整数在计算机内部的表示
    Java核心技术点之接口
    Java核心技术点之内部类
    配置resin web方式部署项目
    rsa加密算法,前后端实现。
    引用百度bcebos jar 503问题
    HashMap get()返回值问题
  • 原文地址:https://www.cnblogs.com/chensisi/p/13261517.html
Copyright © 2011-2022 走看看