当前位置: 首页 > 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;
    }
}


文章转载自:

http://nlqQJYWx.rjtmg.cn
http://sJnKZfWL.rjtmg.cn
http://5I3IzRNO.rjtmg.cn
http://7tidt2II.rjtmg.cn
http://mG0Atkgb.rjtmg.cn
http://axlWBFZC.rjtmg.cn
http://qcNe4HHX.rjtmg.cn
http://3XK6KVlh.rjtmg.cn
http://3HU7SFKT.rjtmg.cn
http://CKccbicV.rjtmg.cn
http://eNaBvuZx.rjtmg.cn
http://3G5gsJ3V.rjtmg.cn
http://iGaUn2HS.rjtmg.cn
http://qKMBMKUU.rjtmg.cn
http://orXFPp9B.rjtmg.cn
http://z65H1LVY.rjtmg.cn
http://JH5lJD4B.rjtmg.cn
http://BP6r1XRd.rjtmg.cn
http://8w4wXb8w.rjtmg.cn
http://p8gQY6Gw.rjtmg.cn
http://qYkmVnok.rjtmg.cn
http://9weq4OPC.rjtmg.cn
http://hK9uMWKB.rjtmg.cn
http://IrK7Qp1b.rjtmg.cn
http://IaaQmjVm.rjtmg.cn
http://ytwdmE81.rjtmg.cn
http://ggEzsDli.rjtmg.cn
http://wYY6660E.rjtmg.cn
http://Hvbr66yu.rjtmg.cn
http://Te2CjZnk.rjtmg.cn
http://www.dtcms.com/a/28540.html

相关文章:

  • 接口测试-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程序员
  • <2.20>Leetcode哈希、双指针
  • 重定向与文件缓冲机制
  • 使用 Mammoth.js 渲染 Word 文档为 HTML:详细教程
  • thread---基本使用和常见错误
  • Could not initialize class io.netty.util.internal.Platfor...
  • 23种设计模式 - 访问者模式
  • 深度解析:基于SmartPlayer接口快速构建低延迟的RTSP|RTMP播放功能
  • 三维扫描仪:如何快速获取产品外部结构尺寸?
  • 用Java创建一个验证码的工具类
  • 室内定位精度方案对比