• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
lvym
博客园    首页    新随笔    联系   管理    订阅  订阅
java 递归查子菜单

 

递归查:

    @Override
    public List<PromotionOrgInfoPO> queryOrgInfo() {
        List<PromotionOrgInfoPO> promotionOrgInfoPOS = pointExchangeDAO.queryOrgInfo();
        List<PromotionOrgInfoPO> rootInfo = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(promotionOrgInfoPOS)) {
            promotionOrgInfoPOS.forEach(promotionOrgInfoPO -> {
                if (Objects.isNull(promotionOrgInfoPO.getParentId())) {
                    rootInfo.add(promotionOrgInfoPO);
                }
            });
            rootInfo.forEach(menu -> {
                List<PromotionOrgInfoPO> childList = getChildMenu(menu.getId(), promotionOrgInfoPOS);
                menu.setChildOrgInfos(childList);
            });
        }
        return rootInfo;
    }

    private List<PromotionOrgInfoPO> getChildMenu(Long id, List<PromotionOrgInfoPO> allMenu) {
        //子菜单
        List<PromotionOrgInfoPO> childList = new ArrayList<>();
        allMenu.forEach(all -> {
            // 遍历所有节点,将所有的父id与传过来的根节点的id比较
            if (all.getParentId().equals(id)) {
                childList.add(all);
            }
        });
        //递归
        childList.forEach(child -> child.setChildOrgInfos(getChildMenu(child.getId(), allMenu)));
        //如果节点下没有子节点,返回一个空List(递归退出)
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

优化:
private List<PromotionOrgInfoPO> getChildMenu(Long id, List<PromotionOrgInfoPO> allMenu) {
// 构建父节点到子节点的映射
Map<Long, List<PromotionOrgInfoPO>> childMap = allMenu.stream()
.collect(Collectors.groupingBy(PromotionOrgInfoPO::getParentId));

return buildChildMenu(id, childMap);
}

private List<PromotionOrgInfoPO> buildChildMenu(Long id, Map<Long, List<PromotionOrgInfoPO>> childMap) {
List<PromotionOrgInfoPO> childList = childMap.getOrDefault(id, Collections.emptyList());

for (PromotionOrgInfoPO child : childList) {
child.setChildOrgInfos(buildChildMenu(child.getId(), childMap));
}

return childList;
}

 

 

常用求和:

Map<String, BigDecimal> currencyCard = paymentVOS.stream().distinct().collect(Collectors.groupingBy(InternalPaymentVO::getDeductCardNo,
                        Collectors.mapping(InternalPaymentVO::getDeductAmount,
                                Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
    int points = internalPaymentVOS.stream().filter(v -> v.getType().equals(92)).mapToInt(InternalPaymentVO::getPmUsedPoints).sum();
     
                BigDecimal pointsMoney = internalPaymentVOS.stream().filter(v -> v.getType().equals(92)).map(InternalPaymentVO::getPmUsedMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                paymentVOS = internalPaymentVOS.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(InternalPaymentVO::getType))), ArrayList::new));
        

 

posted on 2021-11-01 09:30  lvym777  阅读(303)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3