1. 程式人生 > 實用技巧 >lamdba初體驗

lamdba初體驗

假設我們有一個場景:需要查詢指定條件的人員資訊,Person類定義了人員屬性,Utils生成我們需要的人員名單,下面的例子我們來探討下,

Person:

package crelle.test.java.other.beans;

import java.time.LocalDate;

/**
 * @author:crelle
 * @className:Person
 * @version:1.0.0
 * @date:2020/8/15
 * @description:XX
 **/
public class Person {

    public enum Sex {
        MALE, FEMALE
    }
    
private String name; private LocalDate birthday; private Sex gender; private String emailAddress; private int age; public void printPerson() { System.out.println(toString()); } public String getName() { return name; } public void setName(String name) {
this.name = name; } public LocalDate getBirthday() { return birthday; } public void setBirthday(LocalDate birthday) { this.birthday = birthday; } public Sex getGender() { return gender; } public void setGender(Sex gender) { this.gender = gender; }
public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", birthday=" + birthday + ", gender=" + gender + ", emailAddress='" + emailAddress + '\'' + ", age=" + age + '}'; } }

Utils:

package crelle.test.java.other;

import crelle.test.java.other.beans.Order;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:crelle
 * @className:utils
 * @version:1.0.0
 * @date:2020/8/15
 * @description:XX
 **/
public class Utils {
    public static List<Person> getPersonsLists(){
        List<Person> personList = new ArrayList<Person>();
        Person person1 = new Person();
        person1.setAge(15);
        LocalDate localDate1 = LocalDate.of(2020,4,1);
        person1.setBirthday(localDate1);
        person1.setGender(Person.Sex.MALE);
        person1.setEmailAddress("[email protected]");
        person1.setName("tom");
        personList.add(person1);

        Person person2 = new Person();
        person2.setAge(17);
        LocalDate localDate2 = LocalDate.of(2020,6,1);
        person2.setBirthday(localDate2);
        person2.setGender(Person.Sex.MALE);
        person2.setEmailAddress("[email protected]");
        person2.setName("john");
        personList.add(person2);

        Person person3 = new Person();
        person3.setAge(19);
        LocalDate localDate3 = LocalDate.of(2020,8,1);
        person3.setBirthday(localDate3);
        person3.setGender(Person.Sex.FEMALE);
        person3.setEmailAddress("[email protected]");
        person3.setName("lili");
        personList.add(person3);
        return personList;
    }

}

1、獲取年齡為15的人員

方法的入參為人員集合,年齡

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Create Methods That Search for Members That Match One Characteristic
 **/
public class Approach1 {
    public static void main(String[] args) {
        printPersonsOlderThan(Utils.getPersonsLists(),15);
    }

    public static void printPersonsOlderThan(List<Person> roster, int age) {
        for (Person p : roster) {
            if (p.getAge() >= age) {
                p.printPerson();
            }
        }
    }
}

現在我們想獲取年齡在16-25之間的人員

2、獲取年齡在16-25之間的人員

方法的入參為人員集合,年齡範圍

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Create More Generalized Search Methods
 **/
public class Approach2 {
    public static void main(String[] args) {

        printPersonsWithinAgeRange(Utils.getPersonsLists(),16,25);
    }

    public static void printPersonsWithinAgeRange(
            List<Person> roster, int low, int high) {
        for (Person p : roster) {
            if (low <= p.getAge() && p.getAge() < high) {
                p.printPerson();
            }
        }
    }
}

我們做一個改進,把查詢條件做一個抽取,我們抽取一個介面CheckPerson:

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.beans.Person;

/**
 * @author:crelle
 * @className:CheckPerson
 * @version:1.0.0
 * @date:2020/8/15
 * @description:XX
 **/
public interface CheckPerson {
    boolean test(Person p);
}

3、查詢16-25的男性

方法的入參為人員集合,CheckPerson實現類

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Specify Search Criteria Code in a Local Class
 **/
public class Approach3 {
    public static void main(String[] args) {
        printPersons(Utils.getPersonsLists(), new CheckPersonEligibleForSelectiveService());
    }


    public static void printPersons(
            List<Person> roster, CheckPerson tester) {
        for (Person p : roster) {
            if (tester.test(p)) {
                p.printPerson();
            }
        }
    }
}


class CheckPersonEligibleForSelectiveService implements CheckPerson {
    public boolean test(Person p) {
        return p.getGender() == Person.Sex.MALE &&
                p.getAge() >= 16 &&
                p.getAge() <= 25;
    }
}

可以看出我們的條件入參由基本型別演變成了一個介面的實現類,下面我們用匿名類實現

4、查詢16-25的男性

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Specify Search Criteria Code in an Anonymous Class
 **/
public class Approach4 {
    public static void main(String[] args) {

        printPersons(Utils.getPersonsLists(),new CheckPerson() {
                    public boolean test(Person p) {
                        return p.getGender() == Person.Sex.MALE &&
                                p.getAge() >= 16 &&
                                p.getAge() <= 25;
                    }
                }
        );
    }


    public static void printPersons(
            List<Person> roster, CheckPerson tester) {
        for (Person p : roster) {
            if (tester.test(p)) {
                p.printPerson();
            }
        }
    }
}

條件入參我們還可以做改變,我們替換匿名類用lambda表示式來實現

5、查詢16-25的男性

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Specify Search Criteria Code with a Lambda Expression
 **/
public class Approach5 {
    public static void main(String[] args) {
        printPersons(Utils.getPersonsLists(),(Person p) -> p.getGender() == Person.Sex.MALE && p.getAge() >= 16 && p.getAge() <= 25);
    }

    public static void printPersons(
            List<Person> roster, CheckPerson tester) {
        for (Person p : roster) {
            if (tester.test(p)) {
                p.printPerson();
            }
        }
    }
}

我們仔細研究一下CheckPerson介面,返現只有一個抽象方法,輸入為物件,輸出為boolean型別,我們發現和下面的介面test方法很像:

我替換CheckPerson來使用它

6、查詢16-25的男性

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Use Standard Functional Interfaces with Lambda Expressions
 **/
public class Approach6 {
    public static void main(String[] args) {
        printPersonsWithPredicate(Utils.getPersonsLists(),(Person p) -> p.getGender() == Person.Sex.MALE && p.getAge() >= 16 && p.getAge() <= 25);
    }

    public static void printPersonsWithPredicate(
            List<Person> roster, Predicate<Person> tester) {
            for (Person p : roster) {
                if (tester.test(p)) {
                    p.printPerson();
                }
            }
    }
}

我們可以把列印人員明細單獨抽取出來,作為一個入參

7、查詢16-25的男性

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Use Lambda Expressions Throughout Your Application
 **/
public class Approach7 {
    public static void main(String[] args) {
        processPersons(Utils.getPersonsLists(), p -> p.getGender() == Person.Sex.MALE && p.getAge() >= 16 && p.getAge() <= 25, p -> p.printPerson()
        );
    }

    public static void processPersons(
            List<Person> roster,
            Predicate<Person> tester,
            Consumer<Person> block) {
        for (Person p : roster) {
            if (tester.test(p)) {
                block.accept(p);
            }
        }
    }
}

8、查詢16-25的男性,並且列印他們的電子郵箱

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Specify Search Criteria Code with a Lambda Expression
 **/
public class Approach8 {
    public static void main(String[] args) {
        processPersonsWithFunction(
                Utils.getPersonsLists(),
                p -> p.getGender() == Person.Sex.MALE
                        && p.getAge() >= 16
                        && p.getAge() <= 25,
                p -> p.getEmailAddress(),
                email -> System.out.println(email)
        );
    }

    public static void processPersonsWithFunction(
            List<Person> roster,
            Predicate<Person> tester,
            Function<Person, String> mapper,
            Consumer<String> block) {
        for (Person p : roster) {
            if (tester.test(p)) {
                String data = mapper.apply(p);
                block.accept(data);
            }
        }
    }
}

我們用到了下面的函式式介面

T- the type of the input to the function 函式輸入

R- the type of the result of the function 函式輸出

我們使用強大的stream配合lambda來實現查詢16-25的男性,並且列印他們的電子郵箱

9、查詢16-25的男性,並且列印他們的電子郵箱

package crelle.test.java.util.function.lambdatutorials;

import crelle.test.java.other.Utils;
import crelle.test.java.other.beans.Person;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author:crelle
 * @className:Approach1
 * @version:1.0.0
 * @date:2020/8/15
 * @description: Use Aggregate Operations That Accept Lambda Expressions as Parameters
 **/
public class Approach9 {
    public static void main(String[] args) {
        Utils.getPersonsLists()
                .stream()
                .filter(
                        p -> p.getGender() == Person.Sex.MALE
                                && p.getAge() >= 12
                                && p.getAge() <= 25)
                .map(p -> p.getEmailAddress())
                .forEach(email -> System.out.println(email));
    }


}

實現同一個功能程式碼少了多少,自己體會。

完畢!