Lambda引言

Lambda表达式:可以方便我们把方法当做参数传递

package airycode_java8.nice1;


import org.junit.Test;

import java.util.*;

/**
 * Created by admin on 2018/12/28.
 */
public class TestLambda {

    public static void main(String[] args) {
        List<Employee> employees = filterEmployee(employeeList, new FilterEmployeeByAge());
        System.out.println(employees);
        System.out.println("-------------------------------------------");
        List<Employee> employees2 = filterEmployee(employeeList, new FilterEmployeeBySalary());
        System.out.println(employees2);

        System.out.println("=======================");
        test1111();
    }

    //匿名内部类
    public void test(){
        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 testL(){
        Comparator<Integer> com = (x,y)->Integer.compare(x,y);
        TreeSet<Integer> ts = new TreeSet<>(com);
    }



    //准备数据
    static List<Employee> employeeList = Arrays.asList(
            new Employee("张三",18,9999.99, Employee.Status.FREE),
            new Employee("李四",38,5555.55,Employee.Status.BUSY),
            new Employee("王五",50,6666.66,Employee.Status.VOCATION),
            new Employee("赵六",16,3333.33,Employee.Status.FREE),
            new Employee("田七",8,7777.77,Employee.Status.BUSY)
    );
    //需求:获取当前公司员工年龄大于35的员工的信息
    public List<Employee> filterEmployees(List<Employee>employeeList){
        List<Employee> emps = new ArrayList<>();

        for (Employee emp:employeeList) {
            if (emp.getAge() >= 35) {
                emps.add(emp);
            }
        }

        return emps;
    }

    //需求:改变1:获取当前公司员工工资大于5000的员工信息
    public List<Employee> filterEmployees2(List<Employee>employeeList){
        List<Employee> emps = new ArrayList<>();

        for (Employee emp:employeeList) {
            if (emp.getSalary()>=5000) {
                emps.add(emp);
            }
        }

        return emps;
    }

    //优化方式1:设计模式优(策略设计模式)化上述需求的改变
    public static List<Employee> filterEmployee(List<Employee>employeeList,MyPredicate<Employee> mp){
        List<Employee> emps = new ArrayList<>();

        for (Employee emp:employeeList) {
            if (mp.test(emp)) {
                emps.add(emp);
            }
        }

        return emps;
    }

    //优化方式2:匿名内部类
    public static void test1111(){
        List<Employee> list = filterEmployee(employeeList, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary() <= 5000;
            }
        });
        System.out.println(list);
    }

    //优化方式2:匿名内部类
    @Test
    public void test5(){
        List<Employee> list = filterEmployee(employeeList, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary() <= 5000;
            }
        });
        System.out.println(list);
    }

    //优化方式3.Lambda表达式
    @Test
    public void test6(){
        List<Employee> employees = filterEmployee(employeeList, employee -> employee.getSalary() <= 5000);
        employees.forEach(System.out::println);
    }

    //优化方式4.上述代码不存在的写法(Stream API)
    @Test
    public void test7(){
        employeeList.stream().filter(employee -> employee.getSalary()<5000).forEach(System.out::println);
        System.out.println("----------------------------");
        //提取所有的名字
        employeeList.stream().map(Employee::getName).forEach(System.out::println);
    }
}

新建employee类:

package airycode_java8.nice1;

/**
 * Created by admin on 2018/12/28.
 */
public class Employee {

    private String name;
    private int age;
    private double salary;

    private Status status;

    public Employee() {
        super();
    }

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

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

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        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 +
                ", 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;

        if (age != employee.age) return false;
        if (Double.compare(employee.salary, salary) != 0) 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));
        return result;
    }

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


package airycode_java8.nice1;

/**
 * Created by admin on 2018/12/28.
 */
public class FilterEmployeeByAge implements MyPredicate<Employee> {

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

package airycode_java8.nice1;

/**
 * Created by admin on 2018/12/28.
 */
public class FilterEmployeeBySalary implements MyPredicate<Employee> {

    @Override
    public boolean test(Employee employee) {
        return employee.getSalary()>=5000;
    }
}



package airycode_java8.nice1;

/**
 * Created by admin on 2018/12/28.
 */
public interface MyPredicate<T> {

    public boolean test(T t);

}

  

猜你喜欢

转载自www.cnblogs.com/airycode/p/10231597.html
今日推荐