這里是 simpleInfoList?集合,記為集合A(傳值對象)
List<CourseSimpleInfoDTO> simpleInfoList = courseClient.getSimpleInfoList(courseIds);if(simpleInfoList==null){throw new BizIllegalException("當前課程不存在!");}
這里是 learningPlanVOS 集合,記為集合B(被傳值對象)
ArrayList<LearningPlanVO> learningPlanVOS = new ArrayList<>();lessonPageRecords.stream().forEach(new Consumer<LearningLesson>() {@Overridepublic void accept(LearningLesson learningLesson) {LearningPlanVO learningPlanVO = new LearningPlanVO();BeanUtils.copyProperties(learningLesson,learningPlanVO);learningPlanVO.setCourseName(); //課程名稱learningPlanVO.setSections(); //課程章節數量learningPlanVO.setWeekLearnedSections(); //本周已學習章節數learningPlanVOS.add(learningPlanVO);}});
現在需要將集合A中的【課程名稱】以及【課程的章節數量】屬性,挨個對應的傳入到集合B中進行賦值;像平常,大多數人想到的應該是List里面直接套List,這樣即會繁瑣復雜,而且還浪費空間,真實一舉不兩得
所以,這里可以先將集合A轉換為 map 集合(使用 stream 流將其轉換),以課程ID為鍵,以DTO對象為值
然后將集合A轉化后的 map 集合,根據 get 對應的課程ID,獲取到對應的 DTO 對象,然后傳入集合 B 中,進行挨個的賦值操作
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?