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