如题所示,项目中需要将部分数据处理成为树状结构,实现过程如下:
注:也可以使用sql达到该目的,但此处数据不多,故在代码中处理,主要是sql处理不是很会
List<Data> dataAll = service.findAll();
if (CollUtil.isEmpty(dataAll)) {
return new ArrayList<>();
}
List<Data> parentList = dataAll.stream()
.filter(item -> ObjectUtil.isEmpty(item.getParentNumber())).collect(Collectors.toList());
List<Data> childList = dataAll.stream()
.filter(item -> ObjectUtil.isNotEmpty(item.getParentNumber())).collect(Collectors.toList());
return parentList.stream().map(item1 -> {
Data chapterVo = new Data();
BeanUtils.copyProperties(item1, chapterVo);
List<Data> videoVoList = childList.stream()
.filter(item2 -> item1.getBackgroundManagementCategoryNumber().equals(item2.getParentNumber()))
.map(item3 -> {
Data videoVo = new Data();
BeanUtils.copyProperties(item3, videoVo);
return videoVo;
})
.collect(Collectors.toList());
chapterVo.setChildren(videoVoList);
return chapterVo;
}).collect(Collectors.toList());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28