内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Lambda表達式的優(yōu)勢

Lambda表達式的優(yōu)勢

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

    }
2. 用Lambda表達式優(yōu)化代碼
  • 需求1:獲取當(dāng)前公司中員工年齡大于35的員工信息
  • 需求2:獲取當(dāng)前公司中員工工資大于5000的員工信息

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

    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)
    );
    實現(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è)計模式
    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表達式
    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表達式和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);
    }

本文標(biāo)題:Lambda表達式的優(yōu)勢
URL分享:http://www.rwnh.cn/article28/ghdijp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)外貿(mào)建站、電子商務(wù)、域名注冊、網(wǎng)站策劃、網(wǎng)站設(shè)計

廣告

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

手機網(wǎng)站建設(shè)
务川| 土默特左旗| 平邑县| 泊头市| 延长县| 开远市| 同仁县| 安国市| 万山特区| 安康市| 开江县| 朔州市| 嘉善县| 博爱县| 全椒县| 佛教| 昌平区| 江北区| 宿松县| 拜城县| 宁晋县| 自贡市| 什邡市| 古田县| 肥城市| 合水县| 钟祥市| 颍上县| 鹤山市| 民和| 抚州市| 虎林市| 台中市| 芒康县| 沅陵县| 禹城市| 茂名市| 平山县| 新邵县| 英吉沙县| 洛川县|