JDK8特性--Stream(求和,过滤,排序)

本文深入探讨Java8的Stream API,展示如何通过流处理数据集合,包括筛选、映射、排序和分组等操作,同时提供了丰富的代码示例。

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

Stream简介

Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据。

Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。

元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果

这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选,求和, 排序,聚合等。

下图就很形象的表达了将集合转变为流后对流的操作:

 下面我先简单介绍几种有关流的操作。

首先我们需要创建一个java项目,或者其他什么也可。下面的代码是我创建的一个雇员类,用这个来模拟一组数据,然后变成流进行操作。


public class Employee {
    public Employee(Long empno, String ename, Integer salary, Integer deptno) {
        this.empno = empno;
        this.ename = ename;
        this.salary = salary;
        this.deptno = deptno;
    }

    public Long getEmpno() {
        return empno;
    }

    public String getEname() {
        return ename;
    }

    public Integer getSalary() {
        return salary;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setEmpno(Long empno) {
        this.empno = empno;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public void setSalary(Integer salary) {
        this.salary = salary;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    private Long empno; //员工号
    private String ename; //员工姓名

    @Override
    public String toString() {
        return "Employee{" +
                "empno=" + empno +
                ", ename='" + ename + '\'' +
                ", salary=" + salary +
                ", deptno=" + deptno +
                '}';
    }

    private Integer salary; //薪水
    private Integer deptno; //所属部门号
    //此处省略get/set方法、构造方法以及toString方法
}

在main函数中首先建立数据,对创建好的list进行操作

        Employee e1 = new Employee(7369L, "SMITH", 800, 20);
        Employee e2 = new Employee(7499L, "ALLEN", 1600, 30);
        Employee e3 = new Employee(7521L, "WARD", 1250, 30);
        Employee e4 = new Employee(7782L, "CLARK", 2450, 10);
        Employee e5 = new Employee(7876L, "ADAMS", 1100, 20);
        List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5);

forEach方法

用于迭代stream流中的每一个元素

forEach括号中是的lambda表达式,采用这种形式可以对每个对象进行操作。

//打印每个对象的所属部门号
employees.stream().forEach(Employee -> System.out.println(Employee.getDeptno()));
//打印每个对象
employees.stream().forEach(Employee -> System.out.println(Employee));

map方法

//获取所有员工的姓名
List<String> enames = employees.stream().map(employee -> employee.getEname()).collect(Collectors.toList());
enames.stream().forEach(System.out::println);

mapToInt/mapToLong/mapToDouble方法
这几个方法主要用来对stream流中的元素产生单个的统计结果

 //获取所有员工的薪水总和
int totalSalary = employees.stream().mapToInt(employee -> employee.getSalary()).sum();
System.out.println("薪水总和:" + totalSalary);

filter筛选方法

filter方法用于根据设置的条件对stream流中的数据做过滤操作

//获取薪水超过1500的员工
List<Employee> filterEmp = employees.stream().filter(employee -> employee.getSalary()>1500).collect(Collectors.toList());
filterEmp.stream().forEach(System.out::println);

sorted排序方法

默认升序

//按员工的薪水由低到高排序
List<Employee> sortedEmp = employees.stream().sorted(Comparator.comparing(Employee::getSalary)).collect(Collectors.toList());
sortedEmp.stream().forEach(System.out::println);

降序排列(使用了reversed方法)

//降序
List<Employee> sortedEmp1 = employees.stream().sorted(Comparator.comparing(Employee::getSalary).reversed()).collect(Collectors.toList());

多项排序

//多项排序
List<Employee> sortedEmp2 = employees.stream().sorted(Comparator.comparing(Employee::getSalary).reversed().thenComparing(Employee:: getDeptno)).collect(Collectors.toList());

Collectors类

Collectors 类实现了很多归约操作,例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串。

分组

//按员工所属部门号进行分类
Map<Integer, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(employee -> employee.getDeptno()));
for(Map.Entry<Integer, List<Employee>> entry : map.entrySet()) {
    System.out.println("key: " + entry.getKey() + "  value:" + entry.getValue());
}

提示:以上方法可以串联使用。

//获取20号部门员工姓名,按薪水从高到低排序
List<String> names = employees.stream().filter(employee -> employee.getDeptno().equals(20)).sorted(Comparator.comparing(Employee::getSalary).reversed()).map(employee -> employee.getEname()).collect(Collectors.toList());
names.stream().forEach(System.out::println);

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值