99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

Lambda表達式的優勢

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的員工信息

    十年的東麗網站建設經驗,針對設計、前端、開發、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網絡營銷推廣的優勢是能夠根據用戶設備顯示端的尺寸不同,自動調整東麗建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現優雅布局與設計,從而大程度地提升瀏覽體驗。創新互聯從事“東麗網站設計”,“東麗網站推廣”以來,每個客戶項目都認真落實執行。

    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);
    }

本文標題:Lambda表達式的優勢
URL分享:http://www.yijiale78.com/article28/ghdijp.html

成都網站建設公司_創新互聯,為您提供網站建設、外貿建站、電子商務、域名注冊、網站策劃網站設計

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

手機網站建設