<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java8 Stream流遞歸,幾行代碼搞定遍歷樹形結(jié)構(gòu)

          共 2500字,需瀏覽 5分鐘

           ·

          2021-11-15 08:43

          點擊上方“碼農(nóng)突圍”,馬上關(guān)注

          這里是碼農(nóng)充電第一站,回復(fù)“666”,獲取一份專屬大禮包
          真愛,請設(shè)置“星標(biāo)”或點個“在看

          來源:blog.csdn.net/qq_19244927/article/details/106481777

          可能平常會遇到一些需求,比如構(gòu)建菜單,構(gòu)建樹形結(jié)構(gòu),數(shù)據(jù)庫一般就使用父id來表示,為了降低數(shù)據(jù)庫的查詢壓力,我們可以使用Java8中的Stream流一次性把數(shù)據(jù)查出來,然后通過流式處理,我們一起來看看,代碼實現(xiàn)為了實現(xiàn)簡單,就模擬查看數(shù)據(jù)庫所有數(shù)據(jù)到List里面。

          實體類:Menu.java

          /**
          ?*?Menu
          ?*?@author?lcry
          ?*/

          @Data
          @Builder
          public?class?Menu?{
          ????/**
          ?????*?id
          ?????*/

          ????public?Integer?id;
          ????/**
          ?????*?名稱
          ?????*/

          ????public?String?name;
          ????/**
          ?????*?父id?,根節(jié)點為0
          ?????*/

          ????public?Integer?parentId;
          ????/**
          ?????*?子節(jié)點信息
          ?????*/

          ????public?List
          ?childList;


          ????public?Menu(Integer?id,?String?name,?Integer?parentId)?{
          ????????this.id?=?id;
          ????????this.name?=?name;
          ????????this.parentId?=?parentId;
          ????}

          ????public?Menu(Integer?id,?String?name,?Integer?parentId,?List
          ?childList)?{
          ????????this.id?=?id;
          ????????this.name?=?name;
          ????????this.parentId?=?parentId;
          ????????this.childList?=?childList;
          ????}
          ????
          }

          遞歸組裝樹形結(jié)構(gòu):

          ????@Test
          ????public?void?testtree(){
          ????????//模擬從數(shù)據(jù)庫查詢出來
          ????????List
          ?menus?=?Arrays.asList(
          ????????????????new?Menu(1,"根節(jié)點",0),
          ????????????????new?Menu(2,"子節(jié)點1",1),
          ????????????????new?Menu(3,"子節(jié)點1.1",2),
          ????????????????new?Menu(4,"子節(jié)點1.2",2),
          ????????????????new?Menu(5,"根節(jié)點1.3",2),
          ????????????????new?Menu(6,"根節(jié)點2",1),
          ????????????????new?Menu(7,"根節(jié)點2.1",6),
          ????????????????new?Menu(8,"根節(jié)點2.2",6),
          ????????????????new?Menu(9,"根節(jié)點2.2.1",7),
          ????????????????new?Menu(10,"根節(jié)點2.2.2",7),
          ????????????????new?Menu(11,"根節(jié)點3",1),
          ????????????????new?Menu(12,"根節(jié)點3.1",11)
          ????????);

          ????????//獲取父節(jié)點
          ????????List
          ?collect?=?menus.stream().filter(m?->?m.getParentId()?==?0).map(
          ????????????????(m)?->?{
          ????????????????????m.setChildList(getChildrens(m,?menus));
          ????????????????????return?m;
          ????????????????}
          ????????).collect(Collectors.toList());
          ????????System.out.println("-------轉(zhuǎn)json輸出結(jié)果-------");
          ????????System.out.println(JSON.toJSON(collect));
          ????}

          ????/**
          ?????*?遞歸查詢子節(jié)點
          ?????*?@param?root??根節(jié)點
          ?????*?@param?all???所有節(jié)點
          ?????*?@return?根節(jié)點信息
          ?????*/

          ????private?List
          ?getChildrens(Menu?root,?List?all)?{
          ????????List
          ?children?=?all.stream().filter(m?->?{
          ????????????return?Objects.equals(m.getParentId(),?root.getId());
          ????????}).map(
          ????????????????(m)?->?{
          ????????????????????m.setChildList(getChildrens(m,?all));
          ????????????????????return?m;
          ????????????????}
          ????????).collect(Collectors.toList());
          ????????return?children;
          ????}

          格式化打印結(jié)果:


          -?END -

          最近熱文

          ? ?我在美團的八年
          ? ?為什么下載小電影時,經(jīng)常會卡在99%?
          ? ?朝陽群眾盯上了望京A座?舉報996造成交通嚴(yán)重堵塞
          ? ?院士拿布袋領(lǐng)獎歸來,朋友圈刷屏了

          瀏覽 66
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产内射在线观看 | 破坏版无码AV在线播放 | aaaa免费大片 | 要色综合| 淫色淫色网站 |