Java8 Stream的终止操作使用示例

package com.expgiga.Java8;

/**  *  */ public class Employee {
    private String name;
    private int age;
    private double salary;
    private int id;

    private Status status;

    public Employee() {
    }

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public Employee(String name, int age, double salary, Status status) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.status = status;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

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

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                ", id=" + id +
                ", status=" + status +
                '}';
    }

    public Employee(int id) {
        this.id = id;
    }

    public Employee(int id, int age) {
        this.id = id;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (age != employee.age) return false;
        if (Double.compare(employee.salary, salary) != 0) return false;
        if (id != employee.id) return false;
        return name != null ? name.equals(employee.name) : employee.name == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        temp = Double.doubleToLongBits(salary);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + id;
        return result;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public enum Status {
        FREE,
        BUSY,
        VOCATION;
    }
}

package com.expgiga.Java8;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**  * Java8 Stream的终止操作:  *  */ public class TestStreamAPI {
    public static void main(String[] args) {
        /*  * 查找与匹配  * allMatch  * anyMatch  * noneMatch  * findFirst  * findAny  * count  * max  * min  *  */  List<Employee> employeeList = Arrays.asList(
                new Employee("zhangsan", 18, 19999, Employee.Status.FREE),
                new Employee("lisi", 28, 29999, Employee.Status.BUSY),
                new Employee("wangwu", 38, 39999, Employee.Status.VOCATION),
                new Employee("zhaoliu", 16, 17999, Employee.Status.BUSY),
                new Employee("tianqi", 6, 12999, Employee.Status.FREE)
        );

        boolean b1 = employeeList.stream()
                .allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b1); //false   boolean b2 = employeeList.stream()
                .anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b2);

        boolean b3 = employeeList.stream()
                .noneMatch((e) -> e.getStatus().equals(Employee.Status.FREE));
        System.out.println(b3);

        Optional<Employee> op = employeeList.stream()
                .sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
                .findFirst();
        System.out.println(op.get());

        Optional<Employee> op2 = employeeList.parallelStream()
                .filter((e) -> e.getStatus().equals(Employee.Status.FREE))
                .findAny();
        System.out.println(op2.get());

        Long count = employeeList.stream()
                .count();
        System.out.println(count);

        Optional<Employee> op3 = employeeList.stream()
                .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(op3.get());

        Optional<Double> op4 = employeeList.stream()
                .map((Employee::getSalary))
                .min(Double::compare);
        System.out.println(op4.get());
    }
}

    原文作者:u013063153
    原文地址: https://blog.csdn.net/u013063153/article/details/76768930
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞