【Stream流】基础用法—求和、筛选、排序

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/cxh6863/article/details/102634781

stream是java8的新特性,它可以代替for循环,可以加快代码运行速度,使用stream可以写出高效率、干净、简洁的代码。
下边直接讲stream的使用方法,前提是你已经获取到了list的数据(本篇可博客的list是students)。如果您是刚用,没有数据,也可参考该博客最下边的数据准备。

一、求和—mapToInt/mapToLong/mapToDouble

场景:

在一个班级,每个人都有自己的分数,怎样在java中使用代码对所有人的分数进行求和。

方法:

使用mapToInt/mapToLong/mapToDouble方法进行求和
如果是整型就使用mapToInt,
如果是长整型就使用mapToLong,
如果是浮点型就使用mapToDouble

使用:

System.out.println("求和:");
int sumStudentScoreInt = students.stream().mapToInt(student::getScore).sum(); //(1)
//int sumStudentScoreInt = students.stream().mapToInt(student->student.getScore()).sum();//(2)
System.out.println("Int分数和:"+sumStudentScoreInt);
//(1)和(2)只是写法不一样,“student::getScore”和“student->student.getScore()”是等价的,效果是一样的

double sumStudentScoreDouble = students.stream().mapToDouble(student::getScore).sum();
//double sumStudentScoreDouble = students.stream().mapToDouble(student->student.getScore()).sum();
System.out.println("Double分数和:"+sumStudentScoreDouble);

long sumStudentScoreLong = students.stream().mapToLong(student::getScore).sum();
//long sumStudentScoreLong = students.stream().mapToLong(student->student.getScore()).sum();
System.out.println("Long分数和:"+sumStudentScoreLong);

运行结果

在这里插入图片描述

二、筛选—filter

场景:

在一个班级,每个人都有自己的分数,怎样在java中使用代码筛选出某个分数段的所有人。

方法:

使用filter方法进行过滤

使用

System.out.println("过滤:");
List<student> collectStudentFilter = students.stream().filter(student -> student.getScore() > 90).collect(Collectors.toList());
collectStudentFilter.stream().forEach(System.out::println);

运行结果

在这里插入图片描述

三、排序—sorted

场景:

在一个班级,每个人都有自己的分数,怎样在java中使用代码根据分数的高低进行排序。

方法:

使用sorted方法进行排序
升序——默认就是升序
降序——添加reversed()即是降序

使用

//升序
System.out.println("升序:");
List<student> collectStudentSorted = students.stream().sorted(Comparator.comparing(student::getScore)).collect(Collectors.toList());
collectStudentSorted.stream().forEach(System.out::println);
//降序
System.out.println("降序:");
List<student> collectStudentSortedDesc = students.stream().sorted(Comparator.comparing(student::getScore).reversed()).collect(Collectors.toList());
collectStudentSortedDesc.stream().forEach(System.out::println);

运行结果

在这里插入图片描述

四、获取某个字段值—map

场景:

在一个班级,每个人都有自己的分数,怎样在java中使用代码获取这个班每个人的人名。

方法:

使用map获取某个参数

使用

System.out.println("获取人名:");
List<String> collectStudents = students.stream().map(student -> student.getName()).collect(Collectors.toList());
collectStudents.stream().forEach(System.out::println);

运行结果

在这里插入图片描述

数据准备:

1、有一个实体类(student)

package main.java;

public class student {
    private String name;
    private Integer score;
    private String dept;

    public student(String name, Integer score, String dept) {
        this.name = name;
        this.score = score;
        this.dept = dept;
    }

    public String getName() {
        return name;
    }

    public Integer getScore() {
        return score;
    }

    public String getDept() {
        return dept;
    }

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

    public void setScore(Integer score) {
        this.score = score;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", dept='" + dept + '\'' +
                '}';
    }
}

2、实例化几个对象

student studentTYW = new student("佟毓婉",88,"学生");
student studentZTC = new student("周霆琛",96,"老师");
student studentDYT = new student("杜允唐",87,"学生");

3、将这些数据放到list里

List<student> students = Arrays.asList(studentTYW, studentZTC, studentDYT);

这样有了数据,可以对这些数据进行操作了。

猜你喜欢

转载自blog.csdn.net/cxh6863/article/details/102634781
今日推荐