当前位置: 首页 > news >正文

微服务之间调用外键“翻译”的方法概述

写在前面的话:减少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));
}


http://www.mrgr.cn/news/97782.html

相关文章:

  • aws平台练习
  • DFS--
  • 【场景应用2】speech_recognition: 微调语音模型
  • 【后端开发】Spring MVC-常见使用、Cookie、Session
  • Hi Robot——大脑加强版的π0:基于「VLM的高层次推理+ VLA低层次任务执行」的复杂指令跟随及交互式反馈
  • C++中STL学习(一)——向量、栈、堆、集合
  • 操作符详解(下)——包含整形提升
  • 第1节:计算机视觉发展简史
  • 系统分析师(二)--操作系统
  • SQLI打靶
  • Pascal VOC 2012 数据集格式与文件结构
  • Python 网络请求利器:requests 包详解与实战
  • 配置与管理代理服务器
  • 解决前后端时区不一致问题
  • Helm核心概念与常见操作介绍
  • VSCode 常用快捷键
  • 神经网络入门—自定义神经网络续集
  • CSRF漏洞技术解析与实战防御指南
  • 【WRF理论第十七期】单向/双向嵌套机制(含namelist.input详细介绍)
  • SAP ABAP 多线程处理/并行处理的四种方式