filter(e -> e.getSalary() > 7000) 和 forEach(System.out::println) 为什么这么写怎么来的

//为什么这么用e -> e.getSalary() > 7000? System.out::println?
employees.stream().filter(e -> e.getSalary() > 7000).forEach(System.out::println);

 @Test
    public void test() {
        List<Employee> employees = Dates.getEmployees();
//        filter(Predicate p)——接收 Lambda , 从流中排除某些元素。
        //为什么这么用e -> e.getSalary() > 7000? System.out::println?
        employees.stream().filter(e -> e.getSalary() > 7000).forEach(System.out::println);

        System.out.println("******");

        //问题1, e -> e.getSalary() > 7000 解析
        Predicate<Employee> obj  = new Predicate<Employee>() {
            @Override
            public boolean test(Employee e) {
                if (e.getSalary() > 7000){
                    return  true;
                }
                return false;
            }
        };

        Stream<Employee> stream = employees.stream();
        Predicate<Employee> obj1  = e -> e.getSalary() > 7000;
        Stream<Employee> em  = stream.filter(obj1);

//        Employee[] ee =(Employee[]) em.toArray();
//        for (Employee e : ee) {
//            System.out.println(e);
//        }


        // 问题2 System.out::println 解析
        //Consumer中的void accept(T t) 可以引用PrintStream中的void println(T t)。固可以写成forEach(System.out::println);
        Consumer<Employee> ob  = new Consumer<Employee>() {
            @Override
            public void accept(Employee employee) {
                System.out.println(employee);
            }
        };

        //解决问题2
        em.forEach(ob); //em.forEach(System.out::println)
       

//下面是造的数据

package com.lambda.stream;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 
 * @create 2022-02-21 21:30
 */
public class Dates {

    public static List<Employee> getEmployees() {
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1001, "马化腾", 34, 6000.38));
        list.add(new Employee(1002, "马云", 12, 9876.12));
        list.add(new Employee(1003, "刘强东", 33, 3000.82));
        list.add(new Employee(1004, "雷军", 26, 7657.37));
        list.add(new Employee(1005, "李彦宏", 65, 5555.32));
        list.add(new Employee(1006, "比尔盖茨", 42, 9500.43));
        list.add(new Employee(1007, "任正非", 26, 4333.32));
        list.add(new Employee(1008, "扎克伯格", 35, 2500.32));
        return list;
    }
}

package com.lambda.stream;

import java.util.Objects;

/**
 *Employee  雇员
 */
public class Employee {

    private int id;
    private String name;
    private int age;
    private double salary;  //工资

    public Employee(){

    }

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

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

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return id == employee.id && age == employee.age && Double.compare(employee.salary, salary) == 0 && Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, salary);
    }

    protected String getDetails() {
        return id + "\t" + name + "\t" + age+ "\t" +salary;
    }


    @Override
    public String toString() {
       return getDetails();
    }
}

上一篇:请做一个Filter过滤器的hello world最简单的一个例子


下一篇:NVME-MI 学习记录_1 框架