diff --git a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java index 8985d9ae28..1731bcf558 100644 --- a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java @@ -4,7 +4,6 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -124,9 +123,11 @@ public List listPostBy(Integer categoryId) { Assert.notNull(categoryId, "Category id must not be null"); // Find all post ids - Set postIds = new HashSet<>(); - categoryService.listAllByParentId(categoryId).forEach( - x -> postIds.addAll(postCategoryRepository.findAllPostIdsByCategoryId(x.getId()))); + Set postIds = categoryService.listAllByParentId(categoryId) + .stream() + .map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId())) + .flatMap(Set::stream) + .collect(Collectors.toSet()); return postRepository.findAllById(postIds); } @@ -137,9 +138,11 @@ public List listPostBy(Integer categoryId, PostStatus status) { Assert.notNull(status, "Post status must not be null"); // Find all post ids - Set postIds = new HashSet<>(); - categoryService.listAllByParentId(categoryId).forEach(x -> postIds.addAll( - postCategoryRepository.findAllPostIdsByCategoryId(x.getId(), status))); + Set postIds = categoryService.listAllByParentId(categoryId) + .stream() + .map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId(), status)) + .flatMap(Set::stream) + .collect(Collectors.toSet()); return postRepository.findAllById(postIds); } @@ -150,9 +153,11 @@ public List listPostBy(Integer categoryId, Set status) { Assert.notNull(status, "Post status must not be null"); // Find all post ids - Set postIds = new HashSet<>(); - categoryService.listAllByParentId(categoryId).forEach(x -> postIds.addAll( - postCategoryRepository.findAllPostIdsByCategoryId(x.getId(), status))); + Set postIds = categoryService.listAllByParentId(categoryId) + .stream() + .map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId(), status)) + .flatMap(Set::stream) + .collect(Collectors.toSet()); return postRepository.findAllById(postIds); } @@ -168,9 +173,11 @@ public List listPostBy(String slug, Set status) { throw new NotFoundException("查询不到该分类的信息").setErrorData(slug); } - Set postIds = new HashSet<>(); - categoryService.listAllByParentId(category.getId()).forEach(x -> postIds.addAll( - postCategoryRepository.findAllPostIdsByCategoryId(x.getId(), status))); + Set postIds = categoryService.listAllByParentId(category.getId()) + .stream() + .map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId(), status)) + .flatMap(Set::stream) + .collect(Collectors.toSet()); return postRepository.findAllById(postIds); } @@ -186,9 +193,11 @@ public List listPostBy(String slug, PostStatus status) { throw new NotFoundException("查询不到该分类的信息").setErrorData(slug); } - Set postIds = new HashSet<>(); - categoryService.listAllByParentId(category.getId()).forEach(x -> postIds.addAll( - postCategoryRepository.findAllPostIdsByCategoryId(x.getId(), status))); + Set postIds = categoryService.listAllByParentId(category.getId()) + .stream() + .map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId(), status)) + .flatMap(Set::stream) + .collect(Collectors.toSet()); return postRepository.findAllById(postIds); }