if (!documentList.isEmpty()) { List<Long> documentIdList = documentList.stream().map(Document::getCateId).collect(Collectors.toList()); Map<Long, Cate> cateMap = cateMapper.selectBatchIds(documentIdList).stream().collect(Collectors.toMap(Cate::getId, e -> e, (e1, e2) -> e2)); for (Document documentItem : documentList) { documentItem.setCate(cateMap.get(documentItem.getCateId())); }
时间: 2024-04-15 12:24:31 浏览: 12
这段代码的作用是根据文档列表中的分类ID批量查询对应的分类信息,并将查询结果设置到每个文档对象的 cate 属性中。
首先,通过判断 documentList 是否为空,进入条件判断的代码块。
然后,使用 stream() 方法将 documentList 转换为流,并使用 map() 方法获取文档列表中的所有分类ID,存储到 documentIdList 集合中。
接着,使用 cateMapper.selectBatchIds() 方法批量查询文档分类信息,查询条件为 documentIdList 中的分类ID。查询结果以流的形式返回,并使用 collect() 方法将结果转换为 Map<Long, Cate> 类型,其中键为分类ID,值为对应的分类对象。
接下来,通过 for 循环遍历文档列表中的每个文档对象。通过 cateMap.get() 方法根据文档的分类ID获取对应的分类对象,并将获取到的分类对象设置到文档对象的 cate 属性中。
这样,通过批量查询并设置分类信息后,文档对象的 cate 属性就被正确地赋值了。
最后,整个 for 循环结束后,文档列表中的每个文档对象都被设置了正确的分类信息。
相关问题
public List<Document> listDocument(PageInfo<?> pageInfo, Document document) { PageHelper.startPage(pageInfo.getCurrent(), pageInfo.getPageSize()); List<Document> documentList = documentMapper.selectList(Wrappers.<Document>lambdaQuery() .like(StringUtils.isNotBlank(document.getTitle()), Document::getTitle, document.getTitle()) .like(StringUtils.isNotBlank(document.getSummary()), Document::getSummary, document.getSummary()) .eq(document.getCateId() != null, Document::getCateId, document.getCateId()) .eq(document.getDeptId() != null, Document::getDeptId, document.getDeptId()) .eq(document.getActive() != null, Document::getActive, document.getActive()) .eq(document.getRecommend() != null, Document::getRecommend, document.getRecommend()) .eq(document.getCarousel() != null, Document::getCarousel, document.getCarousel()) .orderByDesc(Document::getDateTime) ); if (!documentList.isEmpty()) { List<Long> documentIdList = documentList.stream().map(Document::getCateId).collect(Collectors.toList()); Map<Long, Cate> cateMap = cateMapper.selectBatchIds(documentIdList).stream().collect(Collectors.toMap(Cate::getId, e -> e, (e1, e2) -> e2)); for (Document documentItem : documentList) { documentItem.setCate(cateMap.get(documentItem.getCateId())); } } return documentList; }
这段代码是一个用于根据条件查询文档列表的方法。方法的参数包括一个 PageInfo 对象和一个 Document 对象。PageInfo 是用于分页查询的工具类,包含当前页码和页面大小等信息。Document 是文档的实体类。
在方法内部,首先使用 PageHelper.startPage() 方法设置分页参数。然后使用 documentMapper.selectList() 方法进行查询,查询条件使用了 Wrappers.<Document>lambdaQuery() 来构建。
在 lambdaQuery() 内部,根据传入的 document 对象的属性值来构建查询条件。使用 StringUtils.isNotBlank() 判断属性值是否为空,如果不为空,则使用 like() 方法进行模糊匹配,如果为空则不添加该条件。使用 eq() 方法进行相等匹配。
最后,通过 orderByDesc() 方法按照文档的日期时间进行降序排序。
如果查询结果不为空,则通过 documentList.stream().map().collect(Collectors.toList()) 获取文档列表中的所有文档分类ID,并使用 cateMapper.selectBatchIds() 批量查询对应的文档分类信息。
然后,通过遍历文档列表,并通过 cateMap.get() 方法设置每个文档的分类信息。
最后,返回查询结果 documentList。
优化以下代码: Set<Long> goodsCategoryId = new HashSet<>(); if (!CollectionUtils.isEmpty(goodsCategoryIds)) { Map<Long, CategoryCache> categoryMap2 = this.categoryService.categoryMap(tenantId, goodsCategoryIds); goodsCategoryIds.forEach(c -> { if (Objects.isNull(categoryMap2.get(c))) { return; } else { CategoryCache categoryCache1 = categoryMap2.get(c); String[] split = categoryCache1.getIdPaths().split("-"); List<String> strings = Arrays.asList(split); Set<Long> categoryId = strings.stream().map(Long::parseLong).collect(Collectors.toSet()); goodsCategoryId.addAll(categoryId); } }); }
可以优化的地方有:
1. 使用 lambda 表达式来简化代码。
2. 如果 goodsCategoryIds 不为空,则先获取 categoryMap2,避免重复获取。
3. 使用 containsKey 来判断 Map 中是否存在某个 key。
4. 将 split 和 Arrays.asList(split) 合并为一行代码。
5. 使用 flatMap 来将 List<Long> 转换为 Set<Long>。
6. 将 forEach 替换为 stream。
优化后的代码如下:
```
Set<Long> goodsCategoryId = new HashSet<>();
if (!CollectionUtils.isEmpty(goodsCategoryIds)) {
Map<Long, CategoryCache> categoryMap2 = this.categoryService.categoryMap(tenantId, goodsCategoryIds);
goodsCategoryIds.stream()
.filter(categoryMap2::containsKey)
.map(categoryMap2::get)
.map(CategoryCache::getIdPaths)
.map(idPaths -> Arrays.stream(idPaths.split("-")).map(Long::parseLong).collect(Collectors.toSet()))
.flatMap(Collection::stream)
.forEach(goodsCategoryId::add);
}
```