数据示例
原始扁平的列表如下:
json data: [
{
"chapterTitle":"怎么说员工才爱听",
"chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
"chapterWeight":1,
"courseId":2174,
"id":0,
"lessonCourseId":100,
"lessonTitle":"说话的艺术,第1讲",
"lessonUuid":"82b2e1eb-849d-4450-9a62-49eb69eb2f11",
"lessonWeight":1,
"tenantId":22
},
{
"chapterTitle":"怎么说员工才爱听",
"chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
"chapterWeight":1,
"courseId":2174,
"id":1,
"lessonCourseId":101,
"lessonTitle":"说话的艺术,第2讲",
"lessonUuid":"b9741ea7-6b48-4354-b344-a44c6f147dad",
"lessonWeight":2,
"tenantId":22
},
{
"chapterTitle":"怎么说员工才爱听",
"chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
"chapterWeight":1,
"courseId":2174,
"id":2,
"lessonCourseId":102,
"lessonTitle":"说话的艺术,第3讲",
"lessonUuid":"8848245f-d3b1-4023-a8ec-6015c4008837",
"lessonWeight":3,
"tenantId":22
},
{
"chapterTitle":"怎么说员工才爱听",
"chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
"chapterWeight":1,
"courseId":2174,
"id":3,
"lessonCourseId":103,
"lessonTitle":"说话的艺术,第4讲",
"lessonUuid":"e251fda2-62c6-45e2-96ae-80cdf84bccab",
"lessonWeight":4,
"tenantId":22
},
{
"chapterTitle":"怎么说员工才爱听",
"chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
"chapterWeight":1,
"courseId":2174,
"id":4,
"lessonCourseId":104,
"lessonTitle":"说话的艺术,第5讲",
"lessonUuid":"56b55099-d047-4a65-99b4-deaef7146d62",
"lessonWeight":5,
"tenantId":22
}
]
处理为带层次结构后:
[
{
"children":[
{
"id":0,
"lessonCourseId":100,
"tenantId":22,
"title":"3分钟玩转HSR如何提升房源质量分,第1讲",
"uuid":"c008e373-734b-4647-be17-a9843be731db",
"weight":1
},
{
"id":1,
"lessonCourseId":101,
"tenantId":22,
"title":"3分钟玩转HSR如何提升房源质量分,第2讲",
"uuid":"64540c91-413f-4bfa-a5b0-41c70a451ee7",
"weight":2
},
{
"id":2,
"lessonCourseId":102,
"tenantId":22,
"title":"3分钟玩转HSR如何提升房源质量分,第3讲",
"uuid":"d57ba179-a6f2-4c17-8798-c2a54d63d367",
"weight":3
},
{
"id":3,
"lessonCourseId":103,
"tenantId":22,
"title":"3分钟玩转HSR如何提升房源质量分,第4讲",
"uuid":"16394439-5a17-44c4-b085-6cf2e119d49d",
"weight":4
},
{
"id":4,
"lessonCourseId":104,
"tenantId":22,
"title":"3分钟玩转HSR如何提升房源质量分,第5讲",
"uuid":"57cf2d26-3481-46b8-b3e6-a089eee9fad8",
"weight":5
}
],
"title":"怎么说员工才爱听",
"uuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
"weight":1
}
]
代码示例
处理思路:先分组得到Map
List<DxSeriesCourseChapter> courseChapters = Lists.newArrayList();
for (int i = 0; i < 5; i++) {
DxSeriesCourseChapter data = new DxSeriesCourseChapter();
data.setId(Long.valueOf(i + 0));
data.setTenantId(22L);
data.setCourseId(2174L);
data.setChapterTitle("怎么说员工才爱听");
data.setChapterUuid("a459cb9d-4abf-4607-aa24-92e614a884c3");
data.setChapterWeight(1);
data.setLessonTitle(String.format("说话的艺术,第%s讲", (i + 1)));
data.setLessonWeight((i + 1));
data.setLessonUuid(UUID.randomUUID().toString());
data.setLessonCourseId(Long.valueOf(i + 100));
courseChapters.add(data);
}
logger.info("json data: {}", JSON.toJSONString(courseChapters, true));
List<DxSeriesCourseChapterVo> chapters = courseChapters.stream()
.collect(Collectors.groupingBy(courseChapter -> {
DxSeriesCourseChapterVo chapterVo = new DxSeriesCourseChapterVo();
chapterVo.setTitle(courseChapter.getChapterTitle());
chapterVo.setWeight(courseChapter.getChapterWeight());
chapterVo.setUuid(courseChapter.getChapterUuid());
return chapterVo;
})).entrySet().stream().map(entry -> {
List<DxSeriesCourseChapter> child = entry.getValue();
List<DxSeriesCourseLessonVo> transformedList = child.stream().map(item -> {
DxSeriesCourseLessonVo lessonVo = new DxSeriesCourseLessonVo();
lessonVo.setId(item.getId());
lessonVo.setTitle(item.getLessonTitle());
lessonVo.setUuid(item.getLessonUuid());
lessonVo.setWeight(item.getLessonWeight());
lessonVo.setTenantId(item.getTenantId());
lessonVo.setLessonCourseId(item.getLessonCourseId());
return lessonVo;
}).collect(Collectors.toList());
DxSeriesCourseChapterVo key = entry.getKey();
key.setChildren(transformedList);
return key;
}).collect(Collectors.toList());
logger.info(JSON.toJSONString(chapters, true));