温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Lambda表达式的优势

发布时间:2020-06-23 13:25:19 来源:网络 阅读:566 作者:孤魂1996 栏目:编程语言

Lambda表达式的优势

1. 匿名内部类和Lambda的对比
//原来的匿名内部类
    @Test
    public void test1(){
        Comparator<Integer> comparator = new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };

        TreeSet<Integer> ts = new TreeSet<>(comparator);
    }
//Lambda表达式
    @Test
    public void test2(){
        Comparator<Integer> comparator = (x ,y) -> Integer.compare(x, y);
        TreeSet<Integer> ts = new TreeSet<>(comparator);

    }
2. 用Lambda表达式优化代码
  • 需求1:获取当前公司中员工年龄大于35的员工信息
  • 需求2:获取当前公司中员工工资大于5000的员工信息

    public class Employee {
    private String name;
    private int age;
    private double salary;
    
    public Employee() {
        super();
    }
    
    public Employee(String name, int age, double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    
    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 +
                '}';
    }
    }
    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)
    );
    实现方式
    @Test
    public void test3(){
                //获取年龄大于35岁的员工信息
        List<Employee> employeeList = filterEmployee(employees);
        System.out.println("年龄大于35岁的员工信息:");
        for (Employee employee : employeeList){
            System.out.println(employee);
        }
    
        //获取当前公司中员工工资大于5000的员工信息
        List<Employee> employeeList1 = filterEmployees2(employees);
        System.out.println("工资大于5000的员工信息:");
        for (Employee employee : employeeList1){
            System.out.println(employee);
        }
    }
    
    获取当前公司中员工年龄大于35的员工信息
    public List<Employee> filterEmployee(List<Employee> employeeList){
        List<Employee> emps = new ArrayList<>();
    
        for (Employee emp : employeeList){
            if (emp.getAge() >= 35){
                emps.add(emp);
            }
        }
        return emps;
    }
    
    获取当前公司中员工工资大于5000的员工信息
    public List<Employee> filterEmployees2(List<Employee> employeeList){
    
        ArrayList<Employee> employees = new ArrayList<>();
        for (Employee employee : employeeList){
            if (employee.getSalary() >= 5000){
                employees.add(employee);
            }
        }
        return employees;
    }
    优化方案一:策略设计模式
    public interface MyPredicate<T> {
    
    public boolean test(T t);
    }
    public class FilterEmployeeByAge implements MyPredicate<Employee>{
    @Override
    public boolean test(Employee employee) {
        return employee.getAge() >= 35;
    }
    }
    public class FilterEmployeeBySalary implements MyPredicate<Employee>{
    @Override
    public boolean test(Employee employee) {
        return employee.getSalary() >= 5000;
    }
    }
    @Test
    public void test4(){
        List<Employee> employeeList = filterEmployee(employees, new FilterEmployeeByAge());
        System.out.println("年龄大于35岁的员工信息:");
        for (Employee employee : employeeList){
            System.out.println(employee);
        }
    
        System.out.println("工资大于5000的员工信息:");
        List<Employee> employeeList1 = filterEmployee(this.employees, new FilterEmployeeBySalary());
    
        for (Employee employee : employeeList1){
            System.out.println(employee);
        }
    
    }
    public List<Employee> filterEmployee(List<Employee> employeeList, MyPredicate<Employee> mp){
        ArrayList<Employee> employees = new ArrayList<>();
    
        for (Employee employee : employeeList){
            if (mp.test(employee)){
                employees.add(employee);
            }
        }
        return employees;
    }
    优化方案二:匿名内部类
    public interface MyPredicate<T> {
    
    public boolean test(T t);
    }
    @Test
    public void test5(){
        List<Employee> employeeList = filterEmployee(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() >= 35;
            }
        });
        System.out.println("年龄大于35岁的员工信息:");
        for (Employee employee : employeeList){
            System.out.println(employee);
        }
    
        List<Employee> employeeList1 = filterEmployee(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary() >= 5000;
            }
        });
        System.out.println("工资大于5000的员工信息:");
        for (Employee employee : employeeList1){
            System.out.println(employee);
        }
    }
    优化方案三:Lambda表达式
    public List<Employee> filterEmployee(List<Employee> employeeList, MyPredicate<Employee> mp){
        ArrayList<Employee> employees = new ArrayList<>();
    
        for (Employee employee : employeeList){
            if (mp.test(employee)){
                employees.add(employee);
            }
        }
        return employees;
    }
    @Test
    public void test6(){
        System.out.println("年龄大于35岁的员工信息:");
        List<Employee> employeeList = filterEmployee(employees, (e) -> e.getAge() >= 35);
        employeeList.forEach(System.out::println);
    
        System.out.println("工资大于5000的员工信息:");
        List<Employee> employeeList1 = filterEmployee(employees, (e) -> e.getSalary() >= 5000);
        employeeList1.forEach(System.out::println);
    }
    优化方案四:Lambda表达式和Stream API
    @Test
    public void test7(){
        System.out.println("年龄大于35岁的员工信息:");
        employees.stream()
                 .filter((e) -> e.getAge() >= 35)
                 .forEach(System.out::println);
    
        System.out.println("工资大于5000的员工信息:");
        employees.stream()
                 .filter((e) -> e.getSalary() >= 5000)
                 .forEach(System.out::println);
    }
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI