还不清楚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 {
/**
* 生成指