'recursive Collectors.groupingBy() java streams
Given two simple data container classes:
@Data
@Builder
public class Category {
private Long id;
private String name;
private String codename;
private AccessibilityEnum accessibility;
private String description;
private List<Article> publishedQuestions;
private List<Category> subcategories;
private List<Article> draftQuestions;
private String url;
private Category parent;`
@Data
@Builder
public class SearchResult {
private Long id; //articleId
private String name;
private String slug;
private List<String> tagNames;
private String answerSample;
private String longAnswerSample;
private String lastPublishedUserName;
private Boolean isPublished;
private LocalDate lastPublishedDate;
private Boolean isInternal;
private Category category;
private String url;
Now when performing a search, say we search for the word "password" and we get 10 articles back as a result, within these 10 articles (search Results) that we got back, 3 of them are from let's say HR Category, 2 of those 3 are from the employees' category, which is a sub-category of HR and the other one left is within the salary category, which is itself a sub-category of employees.
I want to have a filtering method that filters by category hierarchy, so i get something like this:
• HR (3)
-- • Employees (2)
----- • Salary (1)
• random category (4)
-- • random sub-category (1)
----- • random sub-category (3)
• another random category (3) \
This is the method that I wrote:
public Map<Category, Long> findSearchResultsHits(String query) { return findAllSearchResults(query).stream().collect(Collectors.groupingBy(SearchResult::getCategory, Collectors.counting())); }
but I only get the original category of the article, not the hierarchy.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
