溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Lambda表達(dá)式的優(yōu)勢(shì)

發(fā)布時(shí)間:2020-06-23 13:25:19 來源:網(wǎng)絡(luò) 閱讀:566 作者:孤魂1996 欄目:編程語言

Lambda表達(dá)式的優(yōu)勢(shì)

1. 匿名內(nèi)部類和Lambda的對(duì)比
//原來的匿名內(nèi)部類
    @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表達(dá)式
    @Test
    public void test2(){
        Comparator<Integer> comparator = (x ,y) -> Integer.compare(x, y);
        TreeSet<Integer> ts = new TreeSet<>(comparator);

    }
2. 用Lambda表達(dá)式優(yōu)化代碼
  • 需求1:獲取當(dāng)前公司中員工年齡大于35的員工信息
  • 需求2:獲取當(dāng)前公司中員工工資大于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)
    );
    實(shí)現(xiàn)方式
    @Test
    public void test3(){
                //獲取年齡大于35歲的員工信息
        List<Employee> employeeList = filterEmployee(employees);
        System.out.println("年齡大于35歲的員工信息:");
        for (Employee employee : employeeList){
            System.out.println(employee);
        }
    
        //獲取當(dāng)前公司中員工工資大于5000的員工信息
        List<Employee> employeeList1 = filterEmployees2(employees);
        System.out.println("工資大于5000的員工信息:");
        for (Employee employee : employeeList1){
            System.out.println(employee);
        }
    }
    
    獲取當(dāng)前公司中員工年齡大于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;
    }
    
    獲取當(dāng)前公司中員工工資大于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;
    }
    優(yōu)化方案一:策略設(shè)計(jì)模式
    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;
    }
    優(yōu)化方案二:匿名內(nèi)部類
    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);
        }
    }
    優(yōu)化方案三:Lambda表達(dá)式
    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);
    }
    優(yōu)化方案四:Lambda表達(dá)式和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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI