public R selectAnswerQuestionTopic(@RequestParam Long topicId){
Map map = new HashMap<String,List>();
List<AnswerQuestionTopic> list = answerQuestionTopicService.selectAnswerQuestionTopic(topicId);
List<AnswerQuestionTopic> result = list.stream().filter(answerQuestionTopic->
answerQuestionTopic.getQuestionParentId() == 0//获取1级疑问
).map((menu)->{
menu.setChild(getChildren(menu,list));
return menu;
}).sorted((menu1,menu2)->{//按照时间的先后进行排序
return (int) (menu2.getCreateDate().getTime()-menu1.getCreateDate().getTime());
}).collect(Collectors.toList());
map.put("QuestionTree",result);
return R.ok().data(map);
}
//==========================工具方法==================================
//递归查找所以疑问的子疑问
private List<AnswerQuestionTopic> getChildren(AnswerQuestionTopic root,List<AnswerQuestionTopic> all){
List<AnswerQuestionTopic> children = all.stream().filter(answerQuestionTopic -> {
return answerQuestionTopic.getQuestionParentId() == root.getId();
}).map(menu->{
menu.setChild(getChildren(menu,all));
return menu;
}).sorted((menu1,menu2)->{
return (int) (menu2.getCreateDate().getTime()-menu1.getCreateDate().getTime());
}).collect(Collectors.toList());
return children;
}
java8新特性stream过滤list集合
于 2022-06-02 13:44:54 首次发布