数据示例

原始扁平的列表如下:

  1. json data: [
  2. {
  3. "chapterTitle":"怎么说员工才爱听",
  4. "chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
  5. "chapterWeight":1,
  6. "courseId":2174,
  7. "id":0,
  8. "lessonCourseId":100,
  9. "lessonTitle":"说话的艺术,第1讲",
  10. "lessonUuid":"82b2e1eb-849d-4450-9a62-49eb69eb2f11",
  11. "lessonWeight":1,
  12. "tenantId":22
  13. },
  14. {
  15. "chapterTitle":"怎么说员工才爱听",
  16. "chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
  17. "chapterWeight":1,
  18. "courseId":2174,
  19. "id":1,
  20. "lessonCourseId":101,
  21. "lessonTitle":"说话的艺术,第2讲",
  22. "lessonUuid":"b9741ea7-6b48-4354-b344-a44c6f147dad",
  23. "lessonWeight":2,
  24. "tenantId":22
  25. },
  26. {
  27. "chapterTitle":"怎么说员工才爱听",
  28. "chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
  29. "chapterWeight":1,
  30. "courseId":2174,
  31. "id":2,
  32. "lessonCourseId":102,
  33. "lessonTitle":"说话的艺术,第3讲",
  34. "lessonUuid":"8848245f-d3b1-4023-a8ec-6015c4008837",
  35. "lessonWeight":3,
  36. "tenantId":22
  37. },
  38. {
  39. "chapterTitle":"怎么说员工才爱听",
  40. "chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
  41. "chapterWeight":1,
  42. "courseId":2174,
  43. "id":3,
  44. "lessonCourseId":103,
  45. "lessonTitle":"说话的艺术,第4讲",
  46. "lessonUuid":"e251fda2-62c6-45e2-96ae-80cdf84bccab",
  47. "lessonWeight":4,
  48. "tenantId":22
  49. },
  50. {
  51. "chapterTitle":"怎么说员工才爱听",
  52. "chapterUuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
  53. "chapterWeight":1,
  54. "courseId":2174,
  55. "id":4,
  56. "lessonCourseId":104,
  57. "lessonTitle":"说话的艺术,第5讲",
  58. "lessonUuid":"56b55099-d047-4a65-99b4-deaef7146d62",
  59. "lessonWeight":5,
  60. "tenantId":22
  61. }
  62. ]

处理为带层次结构后:

  1. [
  2. {
  3. "children":[
  4. {
  5. "id":0,
  6. "lessonCourseId":100,
  7. "tenantId":22,
  8. "title":"3分钟玩转HSR如何提升房源质量分,第1讲",
  9. "uuid":"c008e373-734b-4647-be17-a9843be731db",
  10. "weight":1
  11. },
  12. {
  13. "id":1,
  14. "lessonCourseId":101,
  15. "tenantId":22,
  16. "title":"3分钟玩转HSR如何提升房源质量分,第2讲",
  17. "uuid":"64540c91-413f-4bfa-a5b0-41c70a451ee7",
  18. "weight":2
  19. },
  20. {
  21. "id":2,
  22. "lessonCourseId":102,
  23. "tenantId":22,
  24. "title":"3分钟玩转HSR如何提升房源质量分,第3讲",
  25. "uuid":"d57ba179-a6f2-4c17-8798-c2a54d63d367",
  26. "weight":3
  27. },
  28. {
  29. "id":3,
  30. "lessonCourseId":103,
  31. "tenantId":22,
  32. "title":"3分钟玩转HSR如何提升房源质量分,第4讲",
  33. "uuid":"16394439-5a17-44c4-b085-6cf2e119d49d",
  34. "weight":4
  35. },
  36. {
  37. "id":4,
  38. "lessonCourseId":104,
  39. "tenantId":22,
  40. "title":"3分钟玩转HSR如何提升房源质量分,第5讲",
  41. "uuid":"57cf2d26-3481-46b8-b3e6-a089eee9fad8",
  42. "weight":5
  43. }
  44. ],
  45. "title":"怎么说员工才爱听",
  46. "uuid":"a459cb9d-4abf-4607-aa24-92e614a884c3",
  47. "weight":1
  48. }
  49. ]

代码示例

处理思路:先分组得到Map> 再处理每个K,V

  1. List<DxSeriesCourseChapter> courseChapters = Lists.newArrayList();
  2. for (int i = 0; i < 5; i++) {
  3. DxSeriesCourseChapter data = new DxSeriesCourseChapter();
  4. data.setId(Long.valueOf(i + 0));
  5. data.setTenantId(22L);
  6. data.setCourseId(2174L);
  7. data.setChapterTitle("怎么说员工才爱听");
  8. data.setChapterUuid("a459cb9d-4abf-4607-aa24-92e614a884c3");
  9. data.setChapterWeight(1);
  10. data.setLessonTitle(String.format("说话的艺术,第%s讲", (i + 1)));
  11. data.setLessonWeight((i + 1));
  12. data.setLessonUuid(UUID.randomUUID().toString());
  13. data.setLessonCourseId(Long.valueOf(i + 100));
  14. courseChapters.add(data);
  15. }
  16. logger.info("json data: {}", JSON.toJSONString(courseChapters, true));
  17. List<DxSeriesCourseChapterVo> chapters = courseChapters.stream()
  18. .collect(Collectors.groupingBy(courseChapter -> {
  19. DxSeriesCourseChapterVo chapterVo = new DxSeriesCourseChapterVo();
  20. chapterVo.setTitle(courseChapter.getChapterTitle());
  21. chapterVo.setWeight(courseChapter.getChapterWeight());
  22. chapterVo.setUuid(courseChapter.getChapterUuid());
  23. return chapterVo;
  24. })).entrySet().stream().map(entry -> {
  25. List<DxSeriesCourseChapter> child = entry.getValue();
  26. List<DxSeriesCourseLessonVo> transformedList = child.stream().map(item -> {
  27. DxSeriesCourseLessonVo lessonVo = new DxSeriesCourseLessonVo();
  28. lessonVo.setId(item.getId());
  29. lessonVo.setTitle(item.getLessonTitle());
  30. lessonVo.setUuid(item.getLessonUuid());
  31. lessonVo.setWeight(item.getLessonWeight());
  32. lessonVo.setTenantId(item.getTenantId());
  33. lessonVo.setLessonCourseId(item.getLessonCourseId());
  34. return lessonVo;
  35. }).collect(Collectors.toList());
  36. DxSeriesCourseChapterVo key = entry.getKey();
  37. key.setChildren(transformedList);
  38. return key;
  39. }).collect(Collectors.toList());
  40. logger.info(JSON.toJSONString(chapters, true));