java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn)
思路:
先從集合中找出來(lái)頂級(jí)的菜單,然后遍歷頂級(jí)菜單,找出每個(gè)頂級(jí)菜單的所有子菜單,然后判斷當(dāng)前需要排列的集合是否為空,如果不為空的話,就在遍歷子菜單的下級(jí)菜單,直至沒(méi)有需要排列的菜單。
使用迭代器,符合條件之后將數(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);
? ? ? ? /**
? ? ? ? 判斷子集集合是否為空比遍歷整個(gè)集合是否為空要慢
? ? ? ? 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è)置層級(jí)菜單的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java 遞歸設(shè)置層級(jí)菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Mybatis實(shí)現(xiàn)CRUD
這篇文章主要介紹了SpringBoot整合Mybatis實(shí)現(xiàn)CRUD的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
springboot+thymeleaf打包成jar后找不到靜態(tài)資源的坑及解決
這篇文章主要介紹了springboot+thymeleaf打包成jar后找不到靜態(tài)資源的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java I/O深入學(xué)習(xí)之File和RandomAccessFile
這篇文章主要介紹了Java I/O深入學(xué)習(xí)之File和RandomAccessFile, I/O系統(tǒng)即輸入/輸出系統(tǒng),對(duì)于一門(mén)程序語(yǔ)言來(lái)說(shuō),創(chuàng)建一個(gè)好的輸入/輸出系統(tǒng)并非易事。在充分理解Java I/O系統(tǒng)以便正確地運(yùn)用之前,我們需要學(xué)習(xí)相當(dāng)數(shù)量的類(lèi)。,需要的朋友可以參考下2019-06-06
Spring注解驅(qū)動(dòng)之BeanPostProcessor后置處理器講解
這篇文章主要介紹了Spring注解驅(qū)動(dòng)之BeanPostProcessor后置處理器講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
springmvc字符編碼過(guò)濾器CharacterEncodingFilter的使用
這篇文章主要介紹了springmvc字符編碼過(guò)濾器CharacterEncodingFilter的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-08-08
Java實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)IO版本
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)IO版本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
MyBatis如何使用PageHelper實(shí)現(xiàn)分頁(yè)查詢
這篇文章主要介紹了MyBatis如何使用PageHelper實(shí)現(xiàn)分頁(yè)查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring入門(mén)配置和DL依賴注入實(shí)現(xiàn)圖解
這篇文章主要介紹了Spring入門(mén)配置和DL依賴注入實(shí)現(xiàn)圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Spring事務(wù)管理下synchronized鎖失效問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于Spring事務(wù)管理下synchronized鎖失效問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03

