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

发布于:2025-02-21 ⋅ 阅读:(17) ⋅ 点赞:(0)

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;
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到