Java递归整合菜单结构

方法一

public List<Catalog> getMenuList() {

        List<Catalog> childMenuList = catalogDao.getChildMenuList();
        List<Catalog> parentMenuList = catalogDao.getParentMenuList();
        if (childMenuList != null && childMenuList.size() > 0) {
            // 过滤条件map,指定map预期大小为非顶级类目集合的size
            Map mp = new HashMap(childMenuList.size());
            // 循环顶级类目插入子类目
            parentMenuList.forEach(iepMenuDTO -> {
                getChildMenu(iepMenuDTO, childMenuList, mp);
            });
        }
        return parentMenuList;
    }

    private void getChildMenu(Catalog catalog, List<Catalog> childMenuList, Map map) {

        List<Catalog> childList = new ArrayList<Catalog>();
        childMenuList.stream()
                .filter(c -> !map.containsKey(c.getId()))
                .filter(c -> c.getParentId().equals(catalog.getId()))
                .forEach(c -> {
                // 放入map,递归循环时,可以跳过这个子类目,提高循环效率
                map.put(c.getId(), c.getParentId());
                // 获取当前类目的子类目
                getChildMenu(c, childMenuList, map);
                // 加入字类目集合
                childList.add(c);
            });
        catalog.setChildren(childList);
    }

方法二

public List<Catalog> getMenuList() {

        List<Catalog> resultList = new ArrayList<>();
        List<Catalog> parentMenuList = catalogDao.getParentMenuList();
        parentMenuList.forEach(parentMenu -> {
            Catalog childMenu = getChildMenu1(parentMenu);
            resultList.add(childMenu);
        });

        return resultList;
    }

    private Catalog getChildMenu1(Catalog parentMenu) {

        List<Catalog> childMenuList = catalogDao.getChildMenuList1(parentMenu.getId());
        parentMenu.setChildren(childMenuList);
        childMenuList.stream().forEach(childMenu -> {
            getChildMenu(childMenu);
        });
        return parentMenu;
    }

根据子节点获取父节点数据

	@GetMapping("/getCatalog")
    public List<Catalog> getCatalog() {

        List<Catalog> catalogList = new ArrayList<>();
        List<Catalog> catalogs = catalogMapper.childNodes();
        // 可以使用set对集合进行去重 避免查询对于同一父节点下子节点,查询父节点时出现多次无效查询
        Set<String> set = new HashSet<>();
        for (Catalog catalog : catalogs) {
            catalogList.add(catalog);
            forBack(catalogList, catalog.getParentId(), set);
            catalogList = catalogList.stream().sorted(Comparator.comparing(Catalog::getId)).collect(Collectors.toList());
        }
        return catalogList;
    }

    private void forBack(List<Catalog> catalog, String parentId, Set<String> set) {

        if (!set.contains(parentId)) {
            Catalog catalogPar = catalogMapper.getCatalog(parentId);
            if (!ObjectUtils.isEmpty(catalogPar)) {
                catalog.add(catalogPar);
                forBack(catalog, catalogPar.getParentId(), set);
            }
            set.add(parentId);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值