Stream流方式处理List示例

Stream流方式处理List示例


三个步骤:

1.源头:源数据(集合、数组)转换为Stream流对象
2.管道(流): 对Stream流对象中的元素进行一系列的过滤、排序、分组等中间操作,并返回一个Stream流对象
3.终点: 按需求,对Stream流进行遍历、收集、统计等操作

代码示例:

  • 新建学生成绩实体TestStreamList
package com.ruoyi.web.test.domain;

/**
 * 学生成绩实体
 */
public class Grade {

    /**
     * 学号
     */
    private String stuNo;

    /**
     * 姓名
     */
    private String stuName;

    /**
     * 班级
     */
    private String stuClass;

    /**
     * 语文成绩
     */
    private Double chineseScore;

    /**
     * 数学成绩
     */
    private Double mathScore;

    /**
     * 英语成绩
     */
    private Double englishScore;

    public Grade(String stuNo, String stuName, String stuClass, double chineseScore, double mathScore, double englishScore) {
        this.stuNo = stuNo;
        this.stuName = stuName;
        this.stuClass = stuClass;
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
    }

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuClass() {
        return stuClass;
    }

    public void setStuClass(String stuClass) {
        this.stuClass = stuClass;
    }

    public Double getChineseScore() {
        return chineseScore;
    }

    public void setChineseScore(Double chineseScore) {
        this.chineseScore = chineseScore;
    }

    public Double getMathScore() {
        return mathScore;
    }

    public void setMathScore(Double mathScore) {
        this.mathScore = mathScore;
    }

    public Double getEnglishScore() {
        return englishScore;
    }

    public void setEnglishScore(Double englishScore) {
        this.englishScore = englishScore;
    }

    @Override
    public String toString() {
        return "Grade{" +
                "stuNo='" + stuNo + '\'' +
                ", stuName='" + stuName + '\'' +
                ", stuClass='" + stuClass + '\'' +
                ", chineseScore=" + chineseScore +
                ", mathScore=" + mathScore +
                ", englishScore=" + englishScore +
                '}';
    }
}

  • 使用Stream流方式对List操作
package com.ruoyi.web.test;

import com.ruoyi.web.test.domain.Grade;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Stream流方式处理List示例
 *
 * 1.源头:源数据(集合、数组)转换为Stream流对象
 * 2.管道(流): 对Stream流对象中的元素进行一系列的过滤、排序、分组等中间操作,并返回一个Stream流对象
 * 3.终点: 按需求,对Stream流进行遍历、收集、统计等操作
 *
 */
public class StreamList {

    /**
     * 筛选语文成绩大于60分的同学
     * @param gradeList
     * @return
     */
    public List<Grade> filterByStream(List<Grade> gradeList){
        return gradeList.stream()
                .filter(Objects::nonNull)
                .filter(grade -> grade.getChineseScore() > 60)
                .collect(Collectors.toList());
    }

    /**
     * 按照语文成绩排序
     * @param gradeList
     * @return
     */
    public List<Grade> sortByStream(List<Grade> gradeList){
        return gradeList.stream()
                .filter(Objects::nonNull)
                .sorted(Comparator.comparingDouble(Grade::getChineseScore))
                .collect(Collectors.toList());
    }

    /**
     * 按照班级分组
     * @param gradeList
     * @return
     */
    public Map<String,List<Grade>> groupByStream(List<Grade> gradeList){
        return gradeList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.groupingBy(Grade::getStuClass));
    }

    /**
     * 求语文成绩的平均分
     * @param gradeList
     * @return
     */
    public Double avgByStream(List<Grade> gradeList){
        return gradeList.stream()
                .filter(Objects::nonNull)
                .mapToDouble(Grade::getChineseScore)
                .average()
                .orElseThrow(IllegalStateException::new);
    }

  • 测试结果
package com.ruoyi.web.test;

import com.ruoyi.web.test.domain.Grade;
import org.junit.jupiter.api.Test;

import java.util.*;

public class TestStreamList {

    private static List<Grade> gradeList;

    static {
        Grade aaGrade = new Grade("2022001", "aa", "101", 90.0, 89.0, 70.0);
        Grade bbGrade = new Grade("2022002", "bb", "101", 91.0, 87.0, 75.0);
        Grade ccGrade = new Grade("2022003", "cc", "101", 59.0, 90.0, 78.0);
        Grade ddGrade = new Grade("2022004", "dd", "102", 56.0, 97.0, 83.0);
        Grade eeGrade = new Grade("2022005", "ee", "102", 93.0, 67.0, 65.0);
        gradeList = Arrays.asList(aaGrade, bbGrade, ccGrade, ddGrade, eeGrade);
    }

    @Test
    public void TestFilterByStream(){
        StreamList streamList = new StreamList();
        List<Grade> grades = streamList.filterByStream(gradeList);
        System.out.println(grades.toString());
    }

    @Test
    public void TestSortByStream(){
        StreamList streamList = new StreamList();
        List<Grade> grades = streamList.sortByStream(gradeList);
        System.out.println(grades.toString());
    }

    @Test
    public void TestGroupByStream(){
        StreamList streamList = new StreamList();
        Map<String, List<Grade>> gradesMap = streamList.groupByStream(gradeList);
        System.out.println(gradesMap.toString());
    }

    @Test
    public void TestAvgByStream(){
        StreamList streamList = new StreamList();
        Double avgGrade = streamList.avgByStream(gradeList);
        System.out.println(avgGrade);
    }

你可能感兴趣的:(Java基础,list,java,数据结构)