Java实现 组装菜单树,菜单树转平级list

实体类代码:

@Data
public class AcAppRoleMenuDto {
    /**
     * 菜单编码
     */
    private String menuCode;

    /**
     * 菜单名称
     */
    private String menuName;

    /**
     * 排序
     */
    private Integer orderValue;

    /**
     * 父菜单id, 0 表示顶级菜单
     */
    private Long parentId;

    /**
     * 角色名称
     */
    private String roleName;

    /**
     * 菜单id
     */
    private Long appMenuId;

    /**
     * 角色id
     */
    private Integer appRoleId;

    /**
     * 子节点
     */
    private List<AcAppRoleMenuDto> childrenList;
}

把list菜单转为Map

/**
     * 把list菜单转为Map
     *
     * @param menuList 菜单树组装之前平级且乱序的菜单list
     * @return
     */
    private Map<Long, AcAppRoleMenuDto> listAcAppMenuToMap(List<AcAppRoleMenuDto> menuList) {
        //指定HashMap初始容量,减少扩容的次数,初始容量大小根据具体情况而定
        Map<Long, AcAppRoleMenuDto> map = new HashMap<>(3000);
        if (CollUtil.isNotEmpty(menuList)) {
            for (AcAppRoleMenuDto appMenuDto : menuList) {
                //appMenuId必须是唯一的才可以作为map的key
                Long appMenuId = appMenuDto.getAppMenuId();
                if (appMenuId != null) {
                    map.put(appMenuId, appMenuDto);
                }
            }
        }
        return map;
    }

把菜单组装成一棵树

 /**
     * 把菜单组装成一棵树
     *
     * @param menuList
     * @return
     */
    private List<AcAppRoleMenuDto> treeAcAppRoleMenuDto(List<AcAppRoleMenuDto> menuList) {
        List<AcAppRoleMenuDto> menuDtoResultList = new ArrayList<>(3000);
        if (CollUtil.isNotEmpty(menuList)) {
            Map<Long, AcAppRoleMenuDto> menuDtoMap = this.listAcAppMenuToMap(menuList);
            for (AcAppRoleMenuDto appRoleMenuDto : menuList) {
                Long appMenuId = appRoleMenuDto.getAppMenuId();
                Long parentId = appRoleMenuDto.getParentId();
                if (appMenuId != null && parentId != null) {
                    if (parentId == 0) {
                        menuDtoResultList.add(appRoleMenuDto);
                    } else {
                        AcAppRoleMenuDto appRoleMenuParent = menuDtoMap.get(parentId);
                        if (appRoleMenuParent != null) {
                            List<AcAppRoleMenuDto> childrenList = appRoleMenuParent.getChildrenList();
                            if (childrenList == null) {
                                childrenList = new ArrayList<>();
                                appRoleMenuParent.setChildrenList(childrenList);
                            }
                            childrenList.add(appRoleMenuDto);
                        }
                    }
                }
            }
        }
        return menuDtoResultList;
    }

把菜单树转为平级的list

 /**
     * 把树转为平级的list
     *
     * @param treeMenuList
     * @param menuList
     * @return
     */
    private List<AcAppRoleMenuDto> treeToLevelAcAppRoleMenu(List<AcAppRoleMenuDto> treeMenuList, List<AcAppRoleMenuDto> menuList) {
        if (CollUtil.isNotEmpty(treeMenuList)) {
            for (AcAppRoleMenuDto appRoleMenuDto : treeMenuList) {
                menuList.add(appRoleMenuDto);
                List<AcAppRoleMenuDto> childrenList = appRoleMenuDto.getChildrenList();
                if (CollUtil.isNotEmpty(childrenList)) {
                    this.treeToLevelAcAppRoleMenu(childrenList, menuList);
                }
            }
        }
        return menuList;
    }

应用场景

把菜单树转为平级的list 应用场景

适用于同步自己系统的菜单到其他系统,或者把其他系统的菜单同步到自己的系统

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值