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

后端Java Stream数据流的使用=>代替for循环

API讲解 

  

对比 

示例代码对比

for循环遍历 

package cn.ryanfan.platformback.service.impl;

import cn.ryanfan.platformback.entity.Algorithm;
import cn.ryanfan.platformback.entity.AlgorithmCategory;
import cn.ryanfan.platformback.entity.DTO.AlgorithmInfoDTO;
import cn.ryanfan.platformback.mapper.AlgorithmCategoryMapper;
import cn.ryanfan.platformback.mapper.AlgorithmMapper;
import cn.ryanfan.platformback.service.IAlgorithmService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘一帆
 * @since 2025-02-20
 */
@Service
@RequiredArgsConstructor
public class AlgorithmServiceImpl extends ServiceImpl<AlgorithmMapper, Algorithm> implements IAlgorithmService {

    private final AlgorithmMapper algorithmMapper;
    private final AlgorithmCategoryMapper algorithmCategoryMapper;

    @Override
    public List<AlgorithmInfoDTO> selectAllAlgorithmInfo() {
        //  最终数据
        List<AlgorithmInfoDTO> result = new ArrayList<>();
        //  查询algorithm表数据
        List<Algorithm> algorithmList = algorithmMapper.selectList(null);
        // 查询category表数据 存于Map
        for(Algorithm algorithm : algorithmList){
            AlgorithmInfoDTO algorithmInfoDTO = new AlgorithmInfoDTO();
            algorithmInfoDTO.setId(algorithm.getId());
            algorithmInfoDTO.setName(algorithm.getName());
            algorithmInfoDTO.setStatus(algorithm.getStatus());
            //设置DTO的category_Name 旧方法:在for循环中进行查询(每一次for都在查询) 新方法:将category表新查出=>存于Map=>每次遍历这个Map
            LambdaQueryWrapper<AlgorithmCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AlgorithmCategory::getId,algorithm.getCategoryId());
            algorithmInfoDTO.setCategoryName(algorithmCategoryMapper.selectOne(lambdaQueryWrapper).getName());
            result.add(algorithmInfoDTO);
        }
        return result;
    }
}

stream流式处理

package cn.ryanfan.platformback.service.impl;

import cn.ryanfan.platformback.entity.Algorithm;
import cn.ryanfan.platformback.entity.AlgorithmCategory;
import cn.ryanfan.platformback.entity.DTO.AlgorithmInfoDTO;
import cn.ryanfan.platformback.mapper.AlgorithmCategoryMapper;
import cn.ryanfan.platformback.mapper.AlgorithmMapper;
import cn.ryanfan.platformback.service.IAlgorithmService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘一帆
 * @since 2025-02-20
 */
@Service
@RequiredArgsConstructor
public class AlgorithmServiceImpl extends ServiceImpl<AlgorithmMapper, Algorithm> implements IAlgorithmService {

    private final AlgorithmMapper algorithmMapper;
    private final AlgorithmCategoryMapper algorithmCategoryMapper;

    @Override
    public List<AlgorithmInfoDTO> selectAllAlgorithmInfo() {
        //  查询algorithm表数据
        List<Algorithm> algorithmList = algorithmMapper.selectList(null);
        // 查询category表数据 存于Map 查询所有的分类数据,避免逐个查询
        List<AlgorithmCategory> categoryList = algorithmCategoryMapper.selectList(null);
        Map<Integer,String> categoryMap = categoryList.stream().collect(Collectors.toMap(AlgorithmCategory::getId,AlgorithmCategory::getName));
        //  最终数据
        List<AlgorithmInfoDTO> result  = algorithmList.stream()
                .map(algorithm -> {
                    AlgorithmInfoDTO algorithmInfoDTO = new AlgorithmInfoDTO();
                    algorithmInfoDTO.setId(algorithm.getId());
                    algorithmInfoDTO.setName(algorithm.getName());
                    algorithmInfoDTO.setStatus(algorithm.getStatus());

                    // 通过分类 id 获取分类名称
                    String categoryName = categoryMap.get(algorithm.getCategoryId());
                    algorithmInfoDTO.setCategoryName(categoryName != null ? categoryName : "未知分类");

                    return algorithmInfoDTO;
                })
                .collect(Collectors.toList());
        return result;
    }
}

相关文章:

  • 接口测试-API测试中常用的协议(中)
  • 解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
  • 本地在ollama上部署deepseek或llama大模型
  • 2024华为OD机试真题-恢复数字序列(C++/Java/Python)-E卷-100分
  • Vue 中组件通信的方式有哪些,如何实现父子组件和非父子组件之间的通信?
  • 【含文档+PPT+源码】基于大数据的交通流量预测系统
  • 解决本地模拟IP的DHCP冲突问题
  • NutUI内网离线部署
  • 20250218反函数求导
  • IPv6报头40字节具体怎么分配的?
  • 快速入门Springboot+vue——MybatisPlus快速上手
  • 16 中介者(Mediator)模式
  • 编写测试计划的六大要素是什么
  • Python网络爬虫技术详解文档
  • 1. 面向对象编程:类/对象/继承/多态
  • 【微信小程序开发】元素顶部重叠
  • Java集合框架之ArrayList解析
  • 简识MQ之Kafka、ActiveMQ、RabbitMQ、RocketMQ传递机制
  • 【量化交易】如何预测股票未来走势(基础版)
  • 通义灵码AI程序员
  • 嘉兴地区有人做网站吗/5g网络优化工程师
  • 电商企业网站建设/seo公司多少钱
  • wordpress首页插入广告/唐山seo推广公司
  • 沈阳网站建设渠道/抖音引流推广怎么做
  • jsp网站开发实例教学/学生个人网页制作html
  • 免费做的网站怎么设置域名/国内免费域名注册网站