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

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

作者: 学习微站 | 来源:发表于2022-12-03 17:16 被阅读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);
                    });
                }
    
    

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    干掉

    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;
        }
    
    

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

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

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/jwwqtdtx.html