Stream流(Stream,Lambda)

本文介绍了Java8的Stream流特性,通过实例展示了如何生成流、使用中间操作如filter、limit、skip、distinct、sorted等,以及终结操作如forEach、collect、count等。并结合Lambda表达式,简化了集合数据的处理,如筛选数学成绩90分以上并按成绩降序排列的学生信息。
摘要由CSDN通过智能技术生成

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='吴忠威'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

深知她是一场梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值