寫在前面的話:減少strean流操作,減少多層嵌套for循環。使用普通for循環和map的方式進行轉換,
第一步查詢數據
List<Student> findList = studentDao.findList(findMap);
第二步準備遍歷和賦值
if(CollectionUtil.isNotEmpty(findList)){// 第一次遍歷,取出所有待翻譯的字段,避免重復使用steam流取值Set<String> courseSet = new HashSet<>(16);Set<String> schooldSet = new HashSet<>(16);Set<String> nativePlaceSet = new HashSet<>(16);for (Student student : findList) {// 課程idString courseId = student.getSourceId();if(StringUtil.isNotEmpty(courseId)){courseSet.add(courseId);}// 學校idString schooldId = student.getSchoold();if(StringUtil.isNotEmpty(schooldId)){schooldSet.add(schooldId);}// 籍貫idString nativePlaceId = student.getNativePlace();if(StringUtil.isNotEmpty(nativePlaceId)){nativePlaceSet.add(nativePlaceId);}}// 查詢課程信息、學校信息、籍貫信息,并轉換成mapMap<String,Object> findMap = new HashMap<>(16);findMap.put("courseSet",courseSet);findMap.put("schooldSet",schooldSet);findMap.put("nativePlaceSet",nativePlaceSet);List<Course> courseList = courseDao.findList(findMap);List<Schoold> schooldList = schooldDao.findList(findMap);List<NativePlace> nativePlaceList = nativePlaceDao.findList(findMap);// 轉換成mapMap<String,Course> courseMap = CollectionUtil.isEmpty(courseList) ? new HashMap<>(0) :courseList.stream().collect(Collectors.toMap(Course::getId, course -> course));Map<String,Schoold> schooldMap = CollectionUtil.isEmpty(schooldList) ? new HashMap<>(0) :courseList.stream().collect(Collectors.toMap(Schoold::getId, schoold -> schoold));Map<String,NativePlace> nativePlaceMap = CollectionUtil.isEmpty(nativePlaceList) ? new HashMap<>(0) :courseList.stream().collect(Collectors.toMap(NativePlace::getId, nativePlace -> nativePlace));// 第二次遍歷,填充翻譯后的值for (Student student : findList) {// 課程id-->課程名稱String courseId = student.getSourceId();if(StringUtil.isNotEmpty(courseId)){student.setSourceName(courseMap.get(courseId).getName());}// 學校id-->學校名稱String schooldId = student.getSchoold();if(StringUtil.isNotEmpty(schooldId)){student.setSchooldName(schooldMap.get(schooldId).getName());}// 籍貫id-->籍貫名稱String nativePlaceId = student.getNativePlace();if(StringUtil.isNotEmpty(nativePlaceId)){student.setNativePlaceName(nativePlaceMap.get(nativePlaceId).getName());}}}
結語:僅遍歷2次,減少了stream流取值。用轉map的方式減少了多層for循環。
使用阿里的“通益千問”,指定java17將代碼優化如下:
List<Student> findList = studentDao.findList(findMap);
if (CollectionUtil.isNotEmpty(findList)) {// 單次遍歷收集所有ID(性能優化核心)Set<String> courseSet = new HashSet<>(16);Set<String> schoolSet = new HashSet<>(16);Set<String> nativePlaceSet = new HashSet<>(16);for (Student student : findList) {addIfNotEmpty(courseSet, student.getSourceId());addIfNotEmpty(schoolSet, student.getSchoold());addIfNotEmpty(nativePlaceSet, student.getNativePlace());}// 構建查詢參數Map<String, Object> queryMap = new HashMap<>(16);queryMap.put("courseSet", courseSet);queryMap.put("schoolSet", schoolSet); // 修正拼寫錯誤queryMap.put("nativePlaceSet", nativePlaceSet);// 查詢關聯數據List<Course> courseList = courseDao.findList(queryMap);List<School> schoolList = schoolDao.findList(queryMap); // 修正類名List<NativePlace> nativePlaceList = nativePlaceDao.findList(queryMap);// 轉換為Map(使用輔助方法簡化)Map<String, Course> courseMap = toMap(courseList, Course::getId);Map<String, School> schoolMap = toMap(schoolList, School::getId);Map<String, NativePlace> nativePlaceMap = toMap(nativePlaceList, NativePlace::getId);// 填充學生信息(使用Optional處理空值)for (Student student : findList) {// 課程ID → 課程名稱String courseId = student.getSourceId();student.setSourceName(courseMap.getOrDefault(courseId, new Course()).getName());// 學校ID → 學校名稱String schoolId = student.getSchoold();student.setSchooldName(schoolMap.getOrDefault(schoolId, new School()).getName());// 籍貫ID → 籍貫名稱String nativePlaceId = student.getNativePlace();student.setNativePlaceName(nativePlaceMap.getOrDefault(nativePlaceId, new NativePlace()).getName());}
}// 輔助方法:簡化字段添加邏輯
private void addIfNotEmpty(Set<String> set, String value) {if (StringUtil.isNotEmpty(value)) {set.add(value);}
}// 輔助方法:將列表轉換為Map(避免重復代碼)
private <T> Map<String, T> toMap(List<T> list, Function<T, String> keyExtractor) {return list.stream().collect(Collectors.toMap(keyExtractor, Function.identity(), (existing, replacement) -> existing));
}