Stream流是Java8提供的一个新特性,这个有什么新大陆发现呢,我们先看一个例子
以下内容要先有Lambda表达式基础,不清楚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;
}
}
为了方便代码的复用,就弄了一个学生的工具类StudentUtil,来生成学生的列表,代码为
package com.TestStream;
import java.util.ArrayList;
import java.util.List;
/**
* @author 林高禄
* @create 2020-06-04-17:18
*/
public class StudentUtil {
/**
* 生成指定的学生类的列表,用于测试
*
* @return
*/
public static List<Student> createStudentList() {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student(3, "林高禄", 18, 95.5, 68.0));
studentList.add(new Student(2, "徐辉强", 19, 99.0, 98.0));
studentList.add(new Student(1, "吴忠威", 23, 86.0, 78.5));
studentList.add(new Student(17, "东方不败", 16, 54.0, 47.0));
studentList.add(new Student(9, "西方失败", 45, 94.5, 92.0));
studentList.add(new Student(5, "任我行", 29, 75.0, 97.0));
studentList.add(new Student(9, "独孤求败", 45, 98.5, 86.0));
return studentList;
}
/**
* 打印学生列表里的学生信息
*
* @param studentList
*/
public static void printList(List<Student> studentList) {
for (Student s : studentList) {
System.out.println(s);
}
}
}
筛选出数学成绩为90分以上(含90分)的学生,并且分数按从高的到低排序打印
按照以前的做法,我们的代码如下
package com.TestStream;
import java.util.*;
/**
* @author 林高禄
* @create 2020-06-04-16:51
*/
public class BeforeDemo {
public static void main(String[] args) {
List<Student> studentList = StudentUtil.createStudentList();
// 筛选出数学成绩为90分以上(含90分)的学生,并且分数按从高的到低排序打印
System.out.println("-------原数据------");
StudentUtil.printList(studentList);
// 1、筛选出数学成绩为90分以上(含90分)的学生
List<Student> newList = new ArrayList<>();
for(Student s:studentList){
if(s.getMathScore()>=90){
newList.add(s);
}
}
System.out.println("-------成绩为90分以上(含90分)的数据------");
StudentUtil.printList(newList);
// 2、分数按从高的到低排序
newList.sort(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s2.getMathScore().compareTo(s1.getMathScore());
}
});
// 3、打印
System.out.println("-------分数按从高的到低的数据------");
StudentUtil.printList(newList);
}
}
输出:
-------原数据------
Student{no=3, name='林高禄', age=18, mathScore=95.5, chineseScore=68.0}
Student{no=2, name='徐辉强', age=19, mathScore=99.0, chineseScore=98.0}
Student{no=1, name='吴忠威'