Stream流的高级用法-综合分组求和、组合排序、求极值

本文介绍了Java Stream流的高级用法,包括使用collect进行分组求和(如按姓名、姓名编号组合),利用comparing和thenComparing进行组合排序,以及运用summarizing*方法求极值(如最大值、最小值、平均值)。通过具体的例子展示了这些操作在处理学生数据时的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

还不清楚Steam流的用法的,可以先看这篇Stream流(Stream,Lambda)

我们以下的例子都是基于这个学生类Student来操作,下面是学生类Student的代码

学生属性有:编号,名字,年龄,数学成绩,语文成绩,重写toString方法,重写equals和hashCode方法

package com.TestStream;

/**
 * @author 林高禄
 * @create 2020-06-04-16:47
 */
public class Student {
    private Integer no;
    private String name;
    private Integer age;
    private Double mathScore;
    private Double chineseScore;

    public Student(Integer no, String name, Integer age, Double mathScore, Double chineseScore) {
        this.no = no;
        this.name = name;
        this.age = age;
        this.mathScore = mathScore;
        this.chineseScore = chineseScore;
    }

    public Integer getNo() {
        return no;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getMathScore() {
        return mathScore;
    }

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

    public Double getChineseScore() {
        return chineseScore;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", mathScore=" + mathScore +
                ", chineseScore=" + chineseScore +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        return no != null ? no.equals(student.no) : student.no == null;
    }

    @Override
    public int hashCode() {
        return no != null ? no.hashCode() : 0;
    }
}

为了方便代码的复用,就弄了一个学生的工具类StudentUtil2,来生成学生的列表,代码为

package com.TestStream;

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

/**
 * @author 林高禄
 * @create 2020-06-04-17:18
 */
public class StudentUtil2 {
    /**
     * 生成指
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深知她是一场梦

你打不打赏,我都会一直写博客

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值