(5条消息) map干掉两个for循环案例,空间复杂度优化_学习

2022-12-03  本文已影响0人  学习微站

map干掉两个for循环,空间复杂度优化

//分组遍历
            if (CollectionUtils.isNotEmpty(dictGroupList)) {
                dictGroupList.forEach(dicGroup -> {
                    CollectShopCompareResp.ShopTemplateGroupResp shopTemplateGroupResp =
                            new CollectShopCompareResp.ShopTemplateGroupResp();
                    shopTemplateGroupResp.setDictGroup(dicGroup.getDictGroup());
                    shopTemplateGroupResp.setAttrTitle(dicGroup.getDataGroupText());
                    List<CollectShopCompareResp.ShopTemplateDetailResp> shopDetailList = new LinkedList<>();
                    openShopDetailList.forEach(item -> {
                        String attrKey = item.getAttrKey();
                        SysDataDict sysDataDict = allDataMap.get(attrKey);
                        String dictGroup2 = sysDataDict.getDictGroup();
                        if (dicGroup.equals(dictGroup2)) {
                            shopDetailList.add(item);
                        }
                    });
                    if (CollectionUtils.isNotEmpty(shopDetailList)) {
                        shopTemplateGroupResp.setShopDetailList(shopDetailList);
                    }
                    openShopGroupDetailList.add(shopTemplateGroupResp);
                });
            }

干掉

private List<CollectShopCompareResp.ShopTemplateGroupResp>
                                    generateShopGroupDetail(Map<String, SysDataDict> allDataMap,
                                    List<CollectShopCompareResp.ShopTemplateDetailResp> openShopDetailList) {
        List<CollectShopCompareResp.ShopTemplateGroupResp> openShopGroupDetailList = new LinkedList<>();
        if (CollectionUtils.isNotEmpty(openShopDetailList)) {
            //获取已有分组信息
            List<SysDataDict> dictGroupList = new LinkedList<>();
            openShopDetailList.forEach(item -> {
                String attrKey = item.getAttrKey();
                SysDataDict sysDataDict = allDataMap.get(attrKey);
                dictGroupList.add(sysDataDict);
                item.setDictGroup(sysDataDict.getDictGroup());
            });
            Map<String, List<SysDataDict>> dictGroupMap = dictGroupList.stream()
                    .collect(Collectors.groupingBy(SysDataDict::getDictGroup));
            Map<String, List<CollectShopCompareResp.ShopTemplateDetailResp>> detailMap = openShopDetailList.stream()
                    .collect(Collectors.groupingBy(CollectShopCompareResp.ShopTemplateDetailResp::getDictGroup));
            if (null == dictGroupMap || null == detailMap) {
                return openShopGroupDetailList;
            }
            //分组标题及内容
            dictGroupMap.keySet().forEach(item -> {
                CollectShopCompareResp.ShopTemplateGroupResp shopTemplateGroupResp
                        = new CollectShopCompareResp.ShopTemplateGroupResp();
                shopTemplateGroupResp.setDictGroup(item);
                shopTemplateGroupResp.setAttrTitle(allDataMap.get(item).getDataGroupText());
                shopTemplateGroupResp.setShopDetailList(detailMap.get(item));
                openShopGroupDetailList.add(shopTemplateGroupResp);
            });
        }
        return openShopGroupDetailList;
    }

本文使用 文章同步助手 同步

上一篇下一篇

猜你喜欢

热点阅读