java遞歸設(shè)置層級菜單的實(shí)現(xiàn)
思路:
先從集合中找出來頂級的菜單,然后遍歷頂級菜單,找出每個頂級菜單的所有子菜單,然后判斷當(dāng)前需要排列的集合是否為空,如果不為空的話,就在遍歷子菜單的下級菜單,直至沒有需要排列的菜單。
使用迭代器,符合條件之后將數(shù)據(jù)刪除們可以減少遍歷的次數(shù)
package com.eleven; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import lombok.AllArgsConstructor; import lombok.Data; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** ?* @author zhaojinhui ?* @date 2021/6/4 15:11 ?* @apiNote ?*/ public class ElevenTest { ? ? public static void main(String[] args) { ? ? ? ? TestChild one = new TestChild("1","1",null); ? ? ? ? TestChild two = new TestChild("2","2","1"); ? ? ? ? TestChild sex = new TestChild("3","3","2"); ? ? ? ? List<TestChild> list = new ArrayList<>(3); ? ? ? ? Collections.addAll(list,one,two,sex); ? ? ? ? List<TestChild> tree = getTree(list); ? ? ? ? System.out.println(tree); ? ? } ? ? public static List<TestChild> getTree(List<TestChild> testChildList){ ? ? ? ? List<TestChild> result = new ArrayList<>(); ? ? ? ? for (TestChild testChild : testChildList) { ? ? ? ? ? ? if(StrUtil.isBlank(testChild.getParentId())){ ? ? ? ? ? ? ? ? result.add(testChild); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? testChildList.removeAll(result); ? ? ? ? for (TestChild testChild : result) { ? ? ? ? ? ? setChildren(testChild,testChildList); ? ? ? ? } ? ? ? ? return result; ? ? } ? ? public static void setChildren(TestChild parent,List<TestChild> list){ ? ? ? ? List<TestChild> childList = new ArrayList<>(); ? ? ? ? for(Iterator<TestChild> iterator = list.iterator();iterator.hasNext();){ ? ? ? ? ? ? TestChild next = iterator.next(); ? ? ? ? ? ? if(parent.getCode().equals(next.getParentId())){ ? ? ? ? ? ? ? ? childList.add(next); ? ? ? ? ? ? ? ? iterator.remove(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? parent.setChildren(childList); ? ? ? ? /** ? ? ? ? 判斷子集集合是否為空比遍歷整個集合是否為空要慢 ? ? ? ? if(CollUtil.isNotEmpty(childLlist)) { ? ? ? ? ? ? for (TestChild testChild : childList) { ? ? ? ? ? ? ? ? setChildren(testChild, list); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? */ ? ? ? ? ? ? if(CollUtil.isNotEmpty(list)) { ? ? ? ? ? ? for (TestChild testChild : childList) { ? ? ? ? ? ? ? ? setChildren(testChild, list); ? ? ? ? ? ? } ? ? ? ? } ? ? } } @Data @AllArgsConstructor class TestChild{ ? ? private String name; ? ? private String code; ? ? private String parentId; ? ? List<TestChild> children; ? ? public TestChild(String name,String code,String parentId){ ? ? ? ? this.name = name; ? ? ? ? this.code = code; ? ? ? ? this.parentId = parentId; ? ? } }
到此這篇關(guān)于java遞歸設(shè)置層級菜單的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java 遞歸設(shè)置層級菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java springboot Mongodb增刪改查代碼實(shí)例
這篇文章主要介紹了Java springboot Mongodb增刪改查代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07詳解Java如何優(yōu)雅的實(shí)現(xiàn)異常捕獲
在一個優(yōu)秀的項目中一定少不了對程序流程良好的異常捕獲與日志打印,所以本文主要為大家介紹了如何優(yōu)雅的實(shí)現(xiàn)異常捕獲與日志打印輸出,有需要的可以參考下2023-09-09idea2019版Plugins中搜索不到任何插件的問題解決
本文主要介紹了idea2019版Plugins中搜索不到任何插件的問題解決,插件搜不出來的主要原因是plugins.jetbrains.com ping不通,下面就來介紹一下解決方法,感興趣的可以了解一下2023-09-09SpringBoot日程管理Quartz與定時任務(wù)Task實(shí)現(xiàn)詳解
定時任務(wù)是企業(yè)級開發(fā)中必不可少的組成部分,諸如長周期業(yè)務(wù)數(shù)據(jù)的計算,例如年度報表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報告,還有搶購類活動的商品上架,這些都離不開定時任務(wù)。本節(jié)將介紹兩種不同的定時任務(wù)技術(shù)2022-09-09Java中replace、replaceAll和replaceFirst函數(shù)的用法小結(jié)
相信會java的同學(xué)估計都用過replace、replaceAll、replaceFirst這三個函數(shù),可是,我們真的懂他們嗎?下面通過這篇文章大家再來好好學(xué)習(xí)學(xué)習(xí)下這幾個函數(shù)。2016-09-09如何使用Resttemplate和Ribbon調(diào)用Eureka實(shí)現(xiàn)負(fù)載均衡
這篇文章主要介紹了如何使用Resttemplate和Ribbon調(diào)用Eureka實(shí)現(xiàn)負(fù)載均衡,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03