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
本文使用 文章同步助手 同步
网友评论