list 递归 tree

package cn.picclife.authen.common.bean.domain;

import lombok.Data;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @Author liyueming
 * @CreateTime 2021/3/29
 * @Description 表名 authon_comm_menu_info
 */
@Data
public class AuthonMenu implements  Comparable<AuthonMenu>{
    private String menu_id;
    private String pid;
    private String menu_name;
    private String menu_pname;
    private String url;
    private String remark;
    private String createDate;
    private String level;
    private String order_num;
    //是否逻辑删除
    private String is_del;
    //是否启用
    private String is_enable;

    private List<AuthonMenu> authonMenuChild=new ArrayList<>();

    public void add(AuthonMenu authonMenu){
        authonMenuChild.add(authonMenu);
    }
    public void addAll(List<AuthonMenu> authonMenuList){
        authonMenuChild.addAll(authonMenuList);
    }
    public void remove(AuthonMenu authonMenu){
        authonMenuChild.remove(authonMenu);
    }

    public AuthonMenu(String menu_id, String pid, String menu_name, String menu_pname, String url, String remark, String createDate, String level, String order_num, List<AuthonMenu> authonMenuChild) {
        this.menu_id = menu_id;
        this.pid = pid;
        this.menu_name = menu_name;
        this.menu_pname = menu_pname;
        this.url = url;
        this.remark = remark;
        this.createDate = createDate;
        this.level = level;
        this.order_num = order_num;
        this.authonMenuChild = authonMenuChild;
    }

    public AuthonMenu(String menu_id, String pid, String menu_name, String menu_pname, String url, String remark, String createDate, String level, String order_num) {
        this.menu_id = menu_id;
        this.pid = pid;
        this.menu_name = menu_name;
        this.menu_pname = menu_pname;
        this.url = url;
        this.remark = remark;
        this.createDate = createDate;
        this.level = level;
        this.order_num = order_num;
    }

    public void getMenuListChild(AuthonMenu authonMenu ){
        System.out.println(authonMenu);
        if(hasChildren(authonMenu)){
            for(AuthonMenu e:authonMenu.authonMenuChild){
                getMenuListChild(e);
            }
        }
    }



    public static void main(String[] args) {
        List<AuthonMenu> list =new ArrayList<>();
        AuthonMenu authonMenu11=new AuthonMenu("0", "", "gen", "", "", "String remark", "String createDate", "0", "String order_num");

        AuthonMenu authonMenu=new AuthonMenu("1", "0", "子一代", "父一", "", "String remark", "String createDate", "1", "String order_num");
        AuthonMenu authonMenu1=new AuthonMenu("2", "1", "子二代", "子一代", "", "String remark", "String createDate", "2", "String order_num");
        AuthonMenu authonMenu2=new AuthonMenu("3", "2", "子三代", "子二代", "", "String remark", "String createDate", "3", "String order_num");
        AuthonMenu authonMenu3=new AuthonMenu("4", "1", "子二-2代", "子一代", "", "String remark", "String createDate", "5", "String order_num");
        AuthonMenu authonMenu4=new AuthonMenu("5", "2", "子三-3代", "子二代", "", "String remark", "String createDate", "4", "String order_num");
        AuthonMenu authonMenu5=new AuthonMenu("6", "4", "子四代", "子二-2代", "", "String remark", "String createDate", "4", "String order_num");
        AuthonMenu authonMenu6=new AuthonMenu("7", "5", "子五代", "子四代", "", "String remark", "String createDate", "6", "String order_num");
        list.add(authonMenu11);
        list.add(authonMenu);
        list.add(authonMenu1);
        list.add(authonMenu2);
        list.add(authonMenu3);
        list.add(authonMenu4);
        list.add(authonMenu5);
        list.add(authonMenu6);
        //对list进行升序操作
        list.sort(AuthonMenu::compareTo);
        System.out.println(list);
        //getMenuListChild( list , authonMenu);
        //buildByRecursive(list);

        System.out.println(buildByRecursive(list));


    }
   /**
     * 使用递归方法建树
     * @param menuTrees  子节点集合
     * @return List<MenuTree>
     */
    public static List<AuthonMenu> buildByRecursive(List<AuthonMenu> menuTrees) {
        List<AuthonMenu> trees = new ArrayList<>();
        for (AuthonMenu menuTree : menuTrees) {
            //菜单级别为0,则是一级数据,根据实际情况判断可修改相关关联判断
            if ("0".equals(menuTree.getLevel())) {
                trees.add(findChildren(menuTree,menuTrees));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     * @param menuTree  菜单数对象
     * @param menuTrees  子节点
     * @return MenuTree
     */
    private static AuthonMenu findChildren(AuthonMenu menuTree,List<AuthonMenu> menuTrees) {
        for (AuthonMenu it : menuTrees) {
            if(menuTree.getMenu_id().equals(it.getPid())) {
                if (menuTree.getAuthonMenuChild() == null) {
                    menuTree.setAuthonMenuChild(new ArrayList<AuthonMenu>());
                }
                menuTree.getAuthonMenuChild().add(findChildren(it,menuTrees));
            }
        }
        return menuTree;
    }

    public  static List<AuthonMenu> removeDistinctChild(List<AuthonMenu> list ){
        ArrayList authonMenuList = new ArrayList(list.size());
        for(int i=0;i<list.size();i++){
            if(!authonMenuList.contains(list.get(i)))
                authonMenuList.add(list.get(i));
        }
        return authonMenuList;

    }

    public static  boolean hasChildren(AuthonMenu authonMenu){
        if(authonMenu.authonMenuChild.size()>0){
            return true;
        }
        return  false;
    }

	//升序 降序的话反过来
    @Override
    public int compareTo(AuthonMenu o) {
       return Integer.parseInt(this.level)-Integer.parseInt(o.level);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值