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

