当前位置 博文首页 > m0_51945027的博客:为什么使用 Lambda 表达式

    m0_51945027的博客:为什么使用 Lambda 表达式

    作者:[db:作者] 时间:2021-09-13 16:25

    1-Lambda表达式
    为什么使用 Lambda 表达式
    Lambda是一个匿名函数,我们可以把 Lambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升。

    package day01.com.lm.java8;

    import java.util.Objects;

    public class Employee {
    private int id;
    private String name;
    private Integer age;
    private double salary;
    private Status status;

    public Employee(String name, Integer age, double salary, Status status) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.status = status;
    }
    
    public Employee() {
        super();
    }
    
    public Employee(int id) {
        this.id = id;
    }
    
    public Employee(String name, Integer age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    
    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 getSalary() {
        return salary;
    }
    
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    public Status getStatus() {
        return status;
    }
    
    public void setStatus(Status status) {
        this.status = status;
    }
    
    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                ", status=" + status +
                '}';
    }
    
    @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);
    }
    
    public enum Status {
        FREE,
        BUSY,
        VOCATION
    }
    

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    package day01.com.lm.java8;

    public interface MyPredicate {

    public boolean test(T t);
    

    }
    1
    2
    3
    4
    5
    6
    package day01.com.lm.java8;

    public class FilterEmployeeByAge implements MyPredicate {

    @Override
    public boolean test(Employee t) {
        return t.getAge() >= 35;
    }
    

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package day01.com.lm.java8;

    public class FilterEmployeeBySalary implements MyPredicate {
    @Override
    public boolean test(Employee t) {
    return t.getSalary() >= 5000;
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    package day01.com.lm.java8;

    import org.junit.Test;

    import java.util.*;

    public class TestLambda1 {

    //原来的匿名内部类
    public void test1() {
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
    
        TreeSet<Integer> ts = new TreeSet<>(com);
    }
    
    //Lambda 表达式
    public void test2() {
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        TreeSet<Integer> ts = new TreeSet<>(com);
    }
    
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 5555.99),
            new Employee("王五", 50, 6666.66),
            new Employee("赵六", 16, 3333.33),
            new Employee("田七", 8, 7777.77)
    );
    
    //需求:获取当前公司中员工年龄大于35的员工信息
    @Test
    public void test3() {
        List<Employee> list = filterEmployees(this.employees);
    
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
    
    public List<Employee> filterEmployees(List<Employee> list) {
        List<Employee> emps = new ArrayList<>();
        for (Employee emp : list) {
            if (emp.getAge() >= 35) {
                emps.add(emp);
            }
        }
        return emps;
    }
    
    //需求:获取当前公司中员工工资大于5000的员工信息
    public List<Employee> filterEmployees2(List<Employee> list) {
        List<Employee> emps = new ArrayList<>();
        for (Employee emp : list) {
            if (emp.getSalary() >= 5000) {
                emps.add(emp);
            }
        }
        return emps;
    }
    
    //优化方式一:策略设计模式
    @Test
    public void test4() {
        List<Employee> list = filterEmployee(employees, new FilterEmployeeByAge());
    
        for (Employee employee : list) {
            System.out.println(employee);
        }
    
        System.out.println("-----------------------------------");
    
        List<Employee> list2 = filterEmployee(employees, new FilterEmployeeBySalary());
    
        for (Employee employee : list2) {
            System.out.println(employee);
        }
    }
    
    public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp) {
        List<Employee> emps = new ArrayList<>();
        for (Employee employee : list) {
            if (mp.test(employee)) {
                emps.add(employee);
            }
        }
        return emps;
    }
    
    //优化方式二:匿名内部类
    @Test
    public void test5() {
        List<Employee> list = filterEmployee(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee t) {
                return t.getSalary() <= 5000;
            }
        });
    
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
    
    //优化方式三:Lambda表达式
    @Test
    public void test6() {
        List<Employee> list = filterEmployee(employees, (e) -> e.getSalary() <= 5000);
        list.forEach(System.out::println);
    }
    
    //优化方式四:Stream API
    @Test
    public void test7() {
        employees.stream()
                .filter((e) -> e.getSalary() >= 5000)
                .limit(2)
                .forEach(System.out::println);
    
        System.out.println("----------------------------------------");
    
        employees.stream()
                .map(Employee::getName)
                .forEach(System.out::println);
    }
    

    }

    cs