<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>

          別再寫一堆的 for 循環(huán)了!Java 8 中的 Stream 輕松遍歷樹(shù)形結(jié)構(gòu),是真的牛逼!

          共 2581字,需瀏覽 6分鐘

           ·

          2022-03-10 18:30

          點(diǎn)擊關(guān)注公眾號(hào),Java干貨及時(shí)送達(dá)

          可能平常會(huì)遇到一些需求,比如構(gòu)建菜單,構(gòu)建樹(shù)形結(jié)構(gòu),數(shù)據(jù)庫(kù)一般就使用父id來(lái)表示,為了降低數(shù)據(jù)庫(kù)的查詢壓力,我們可以使用Java8中的Stream流一次性把數(shù)據(jù)查出來(lái),然后通過(guò)流式處理。

          ?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é)點(diǎn)?*?@param?root??根節(jié)點(diǎn)?*?@param?all???所有節(jié)點(diǎn)?*?@return?根節(jié)點(diǎn)信息?*/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é)果:" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" style="color: rgb(58, 58, 58);" data-linktype="2">我們一起來(lái)看看,代碼實(shí)現(xiàn)為了實(shí)現(xiàn)簡(jiǎn)單,就模擬查看數(shù)據(jù)庫(kù)所有數(shù)據(jù)到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é)點(diǎn)?*?@param?root??根節(jié)點(diǎn)?*?@param?all???所有節(jié)點(diǎn)?*?@return?根節(jié)點(diǎn)信息?*/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é)果:" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">實(shí)體類:Menu.java

          ?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é)點(diǎn)?*?@param?root??根節(jié)點(diǎn)?*?@param?all???所有節(jié)點(diǎn)?*?@return?根節(jié)點(diǎn)信息?*/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é)果:" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">/**
          ?*?Menu
          ?*
          ?*?@author?lcry
          ?*?@date?2020/06/01?20:36
          ?*/
          ????@Data
          ????@Builder
          ????public?class?Menu?{
          ????/**
          ?????*?id
          ?????*/
          ?????public?Integer?id;
          ?????/**
          ?????*?名稱
          ?????*/
          ?????public?String?name;
          ?????/**
          ?????*?父id?,根節(jié)點(diǎn)為0
          ?????*/
          ?????public?Integer?parentId;
          ?????/**
          ?????*?子節(jié)點(diǎn)信息
          ?????*/
          ?????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;
          ????}

          }

          ?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é)點(diǎn)?*?@param?root??根節(jié)點(diǎn)?*?@param?all???所有節(jié)點(diǎn)?*?@return?根節(jié)點(diǎn)信息?*/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é)果:" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">遞歸組裝樹(shù)形結(jié)構(gòu):

          ?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é)點(diǎn)?*?@param?root??根節(jié)點(diǎn)?*?@param?all???所有節(jié)點(diǎn)?*?@return?根節(jié)點(diǎn)信息?*/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é)果:" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">@Test
          public?void?testtree(){
          ????//模擬從數(shù)據(jù)庫(kù)查詢出來(lái)
          ????List
          ?menus?=?Arrays.asList(
          ????????????new?Menu(1,"根節(jié)點(diǎn)",0),
          ????????????new?Menu(2,"子節(jié)點(diǎn)1",1),
          ????????????new?Menu(3,"子節(jié)點(diǎn)1.1",2),
          ????????????new?Menu(4,"子節(jié)點(diǎn)1.2",2),
          ????????????new?Menu(5,"根節(jié)點(diǎn)1.3",2),
          ????????????new?Menu(6,"根節(jié)點(diǎn)2",1),
          ????????????new?Menu(7,"根節(jié)點(diǎn)2.1",6),
          ????????????new?Menu(8,"根節(jié)點(diǎn)2.2",6),
          ????????????new?Menu(9,"根節(jié)點(diǎn)2.2.1",7),
          ????????????new?Menu(10,"根節(jié)點(diǎn)2.2.2",7),
          ????????????new?Menu(11,"根節(jié)點(diǎn)3",1),
          ????????????new?Menu(12,"根節(jié)點(diǎn)3.1",11)
          ????);

          ????//獲取父節(jié)點(diǎn)
          ????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é)點(diǎn)
          ?*?@param?root??根節(jié)點(diǎn)
          ?*?@param?all???所有節(jié)點(diǎn)
          ?*?@return?根節(jié)點(diǎn)信息
          ?*/
          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;
          }

          ?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é)點(diǎn)?*?@param?root??根節(jié)點(diǎn)?*?@param?all???所有節(jié)點(diǎn)?*?@return?根節(jié)點(diǎn)信息?*/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é)果:" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" style="color: rgb(58, 58, 58);" data-linktype="2">格式化打印結(jié)果:

          原文鏈接:https://blog.csdn.net/qq_19244927/article/details/106481777/

          版權(quán)聲明:本文為CSDN博主「Lcry」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。








          Spring Cloud 爆高危漏洞,趕緊修復(fù)!
          2021 年發(fā)生的 10 件技術(shù)大事??!
          23 種設(shè)計(jì)模式實(shí)戰(zhàn)(很全)
          換掉 Log4j2!tinylog 橫空出世
          再見(jiàn)單身狗!Java 創(chuàng)建對(duì)象的 6 種方式
          阿里為什么推薦使用 LongAdder?
          重磅官宣:Redis 對(duì)象映射框架來(lái)了!!
          別再寫爆爆爆炸類了,試試裝飾器模式!
          程序員精通各種技術(shù)體系,45歲求職難!
          Spring Boot 3.0 M1 發(fā)布,正式棄用 Java 8
          Spring Boot 學(xué)習(xí)筆記,這個(gè)太全了!



          關(guān)注Java技術(shù)??锤喔韶?/strong>



          獲取 Spring Boot 實(shí)戰(zhàn)筆記!
          瀏覽 29
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  五月丁香黄色电影 | 日韩精品免费在线观看 | 国产三级日本三级国产三级 | 多人操逼 | 色婷亚洲五月天 |