Lambda Expression In Java


 題記
在閱讀JDK源碼java.util.Collections的時候在UnmodifiableCollection類中看到了這么一段代碼:

    public void forEach(Consumer<? super E> action) {
        c.forEach(action);
    }

Consumer的源碼如下:

    @FunctionalInterface
    public interface Consumer<T> {
        void accept(T t);
        default Consumer<T> andThen(Consumer<? super T> after) {
            Objects.requireNonNull(after);
            return (T t) -> { accept(t); after.accept(t); };
        }
    }

乍一看讓我費解了一下,但是回過神來發現這不就是Java8的新特性Lambda表達式嗎。原來對於這些新特性只是了解一下,沒注意到在JDK源碼中也使用到了,所以抽時間看了一下Java的Lambda表達式。

Lambda演算
Lambda演算在wiki上的非形式化表述如下:


    在λ演算中,每個表達式都代表一個函數,這個函數有一個參數,並且返回一個值。不論是參數和返回值,也都是一個單參的函數。可以這么說,λ演算中,只有一種“類型”,那就是這種單參函數。
    函數是通過λ表達式匿名地定義的,這個表達式說明了此函數將對其參數進行什么操作。例如,“加2”函數f(x)= x + 2可以用lambda演算表示為λx.x + 2 (或者λy.y + 2,參數的取名無關緊要)而f(3)的值可以寫作(λx.x + 2) 3。函數的應用(application)是左結合的:f x y =(f x) y。
    考慮這么一個函數:它把一個函數作為參數,這個函數將被作用在3上:λf.f 3。如果把這個(用函數作參數的)函數作用於我們先前的“加2”函數上:(λf.f 3)(λx.x+2),則明顯地,下述三個表達式:
        (λf.f 3)(λx.x+2) 與 (λx.x + 2) 3 與 3 + 2
    是等價的。有兩個參數的函數可以通過lambda演算這么表達:一個單一參數的函數的返回值又是一個單一參數的函數(參見Currying)。例如,函數f(x, y) = x - y可以寫作λx.λy.x - y。下述三個表達式:
        (λx.λy.x - y) 7 2 與 (λy.7 - y) 2 與 7 - 2
    也是等價的。然而這種lambda表達式之間的等價性無法找到一個通用的函數來判定。


詳細的形式化表述請跳轉Lambda演算

Java中的Lambda表達式
在Java中Lambda表達式可以有多個參數,在JSR335-FINAL(Java Specification Requests)中對Java中Lambda表達式的形式定義如下:

    LambdaExpression:
          LambdaParameters '->' LambdaBody

    LambdaParameters:
          Identifier
          '(' FormalParameterListopt ')'
          '(' InferredFormalParameterList ')'

    InferredFormalParameterList:
         Identifier
          InferredFormalParameterList ',' Identifier

    LambdaBody:
          Expression
          Block

    The following definitions from 8.4.1 are repeated here for convenience:

    FormalParameterList:
          LastFormalParameter
          FormalParameters ',' LastFormalParameter

    FormalParameters:
          FormalParameter
          FormalParameters, FormalParameter

    FormalParameter:
          VariableModifiersopt Type VariableDeclaratorId

    LastFormalParameter:
          VariableModifiersopt Type '...' VariableDeclaratorId
          FormalParameter

舉例如下 Examples of lambda expressions:

    () -> {}                     // No parameters; result is void
    () -> 42                     // No parameters, expression body
    () -> null                   // No parameters, expression body
    () -> { return 42; }         // No parameters, block body with return
    () -> { System.gc(); }       // No parameters, void block body
    () -> {
          if (true) return 12;
          else {
            int result = 15;
            for (int i = 1; i < 10; i++)
                  result *= i;
            return result;
          }
    }                          // Complex block body with returns
    (int x) -> x+1             // Single declared-type parameter
    (int x) -> { return x+1; } // Single declared-type parameter
    (x) -> x+1                 // Single inferred-type parameter
    x -> x+1                   // Parens optional for single inferred-type case
    (String s) -> s.length()   // Single declared-type parameter
    (Thread t) -> { t.start(); } // Single declared-type parameter
    s -> s.length()              // Single inferred-type parameter
    t -> { t.start(); }          // Single inferred-type parameter
    (int x, int y) -> x+y      // Multiple declared-type parameters
    (x,y) -> x+y               // Multiple inferred-type parameters
    (final int x) -> x+1       // Modified declared-type parameter
    (x, final y) -> x+y        // Illegal: can't modify inferred-type parameters
    (x, int y) -> x+y          // Illegal: can't mix inferred and declared types

注意,在形式參數中推導參數和聲明參數不能混用。(Inferred-type parameters的類型是編譯的時候從上下問中推斷出來的,比如說是借口定義時指定的參數)

Java SE 8: Lambda Quick Start
以下例子摘自 Oracle-Java SE 8: Lambda Quick Start

Runnable Lambda

    public class LambdaTest {
        public static void main(String[] args) {
            LambdaTest LT = new LambdaTest();
            LT.runnableTest();
            LT.comparatorTest();
        }
        public void runnableTest() {
            System.out.println("=== RunnableTest ===");
            // Anonymous Runnable
            Runnable r1 = new Runnable() {
                @Override
                public void run() {
                    System.out.println("Hello world one!");
                }
            };
            // Lambda Runnable
            Runnable r2 = () -> {
                System.out.println("Hello world two!");
                System.out.println("Hello world three!");
            };
            // Run em!
            r1.run();
            r2.run();
        }
    }

上面代碼用Lambda表達式代替了*New*操作和*run*方法的定義,使得代碼更為簡潔。

Comparator Lambda

    class Person {
        public String surName;
        public Person(String surName) {
            super();
            this.surName = surName;
        }
        public void printName() {
            System.out.println(this.surName);
        }
    }

    public void comparatorTest() {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("B"));
        personList.add(new Person("A"));

        //        // Sort with Inner Class
        //        Collections.sort(personList, new Comparator<Person>() {
        //            public int compare(Person p1, Person p2) {
        //                return p1.surName.compareTo(p2.surName);
        //            }
        //        });
        //
        //        System.out.println("=== Sorted Asc SurName ===");
        //        for (Person p : personList) {
        //             p.printName();
        //        }

        // Use Lambda instead
        // Print Asc
        System.out.println("=== Sorted Asc SurName ===");
        Collections.sort(personList, (p1, p2) -> p1.surName.compareTo(p2.surName));
        for (Person p : personList) {
            p.printName();
        }

        // Print Desc
        System.out.println("=== Sorted Desc SurName ===");
        Collections.sort(personList, (p1, p2) -> p2.surName.compareTo(p1.surName));
        for (Person p : personList) {
            p.printName();
        }
    }

這里則是用Lambda表達式代替了匿名的對象*Comparator*的作用。

Function
原先我以為Lambda表達式的加入只是一個簡單的語法糖,但是后面發現還有更多的語法糖。設想一下如果你需要對一個List的數據做判斷和篩選,通常我們會按照下面這種一般做法。

    public class Person {
        public String givenName;
        public String surName;
        public int age;
        public Gender gender;
        public String eMail;
        public String phone;
        public String address;
        //getters and setters
        //...
    }

    public class RoboContactMethods2 {
        public void callDrivers(List<Person> pl){
               for(Person p:pl){
                 if (isDriver(p)){
                       roboCall(p);
                 }
           }
        }

           public boolean isDriver(Person p){
               return p.getAge() >= 16;
           }

           public void roboCall(Person p){
               System.out.println("Calling " + p.getGivenName() + " " + p.getSurName() + " age " + p.getAge() + " at " + p.getPhone());
         }
    }

這樣如果有多個過濾條件的需求,就需要實現更多的判斷函數,那么更文藝一些的做法是這樣的(還是用上面的Person對象舉例):

    public interface MyTest<T> {
           public boolean test(T t);
    }

    public class RoboContactAnon {
           public void phoneContacts(List<Person> pl, MyTest<Person> aTest){
             for(Person p:pl){
                   if (aTest.test(p)){
                     roboCall(p);
                   }
             }
           }

           public void roboCall(Person p){
             System.out.println("Calling " + p.getGivenName() + " " + p.getSurName() + " age " + p.getAge() + " at " + p.getPhone());
           }

           public static void main (String[] args) {
            //get PersonList for testing.
               List<Person> pl = getInitedPersonList();
            RoboContactAnon rca = new RoboContactAnon();
            rca.phoneContacts(pl, (p) -> p.getAge() > 16);
           }
    }

我們這里使用了一個自定義的*MyTest*接口,但是其實我們不需要自己定義這個接口,因為在Java SE 8中,JDK為我們提供了一系列的接口供我們使用,比如我們的*MyTest*接口就可以用系統提供的*Predicte*接口進行替代,它的定義跟MyTest類似:

    public interface Predicate<T> {
        public boolean test(T t);
    }

除了Predicte,JDK還提供了一系列的接口供我們在不同的場景使用。它們在java.util.function包中。下面是列舉的是JDK提供的一部分接口:

    - Predicate: A property of the object passed as argument
    - Consumer: An action to be performed with the object passed as argument
    - Function: Transform a T to a U
    - Supplier: Provide an instance of a T (such as a factory)
    - UnaryOperator: A unary operator from T -> T
    - BinaryOperator: A binary operator from (T, T) -> T

Collections
除了上面提到的語法糖,和java.util.function包以外,Java SE 8還增加了java.util.stream,這是對Collections對象起到了一定的增強。考慮以下場景“你需要對一個List根據一定的條件對元素進行過濾,然后求過濾后元素某個屬性的平均值”。我們的做法一般是這樣:

    //仍舊以List<Person>舉例
    double sum = 0;
    int count = 0;
    for (Person p : personList) {
        if (p.getAge() > 0) {
            sum += p.getAge();
            cout++;
        }
    }
    double average = count > 0 ? sum/average : 0;

如果我們使用stream的話,就可以用更文藝一點的寫法:

    // Get average of ages
    OptionalDouble averageAge = pl
             .stream()
             .filter((p) -> p.getAge() > 16)
             .mapToDouble(p -> p.getAge())
             .average();

可以看到這樣寫的話代碼確實更簡潔了,它把List轉換成一個Stream,然后對元素進行操作。而且如果我們做的操作對元素的順序沒有要求那么我們可以將stream()方法換成parallelStream()方法,這樣可以得到一個可以並行處理的流,當我們對元素進行處理的時候,JVM會把這個流進行划分,對每一個部分並行的進行處理,然后再進行歸並,這樣可以提高處理的效率,而這些對開發人員是透明的。

划分,映射,歸並,這些聽起來有沒有覺得很熟悉,對,這就是MapReduce,只是跟Hadoop用機器作為處理節點不一樣的是這里對於划分的處理是在一個JVM里面進行的。在java.util.stream中給我們提供了一個通用的reduce()方法:

     <U> U reduce(U identity,
              BiFunction<U, ? super T, U> accumulator,
              BinaryOperator<U> combiner);

    int sumOfWeights = widgets.stream()
                              .reduce(0,
                              (sum, b) -> sum + b.getWeight()),
                            Integer::sum);

其中identity是一個reduce的初始值,如果沒有元素進行reduce的話則返回identitiy值,如果有元素進行reduce則在identity上進行累加。accumulator是一個累加器,負責把部分結果與另一個元素進行累加,combiner則是一個合並器,負責把不同部分的子結果進行合並,取得最終的結果。這里如果所進行的運算對元素的元素沒有要求的話我們可以使用parallelStream(),取得一個並行的流,這樣才能對流進行划分和並行處理,充分發揮這些新特性的性能。

將一個輸入的collection做轉換也是可以的比如下面的例子就返回了元素中某個屬性的List:

     <R> R collect(Supplier<R> supplier,
               BiConsumer<R, ? super T> accumulator,
               BiConsumer<R, R> combiner);

     ArrayList<String> strings = stream.collect(() -> new ArrayList<>(),
                                                (c, e) -> c.add(e.toString()),
                                                (c1, c2) -> c1.addAll(c2));

     List<String> strings = stream.map(Object::toString)
                                  .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

有可能你覺得這不是KV對操作,不像MapReduce,那么你可以將結果映射成一個Map,這就是妥妥的KV對了,這個操作需要使用到groupingBy(Collection collection)方法:

     Collector<Employee, ?, Integer> summingSalaries
         = Collectors.summingInt(Employee::getSalary);

     Map<Department, Integer> salariesByDept
         = employees.stream().collect(Collectors.groupingBy(Employee::getDepartment,
                                                            summingSalaries));

以上只是對於stream的簡單舉例,詳情請閱讀[JSR335-FINAL]中關於java.util.stream的部分。

后記
起初我認為這些新特性只是一些語法糖,現在我也還認為這個特性是個語法糖。雖然在開發過程中很少用到這個新特性(甚至都不會用到),但是了解這些新特性總是沒有壞除的,恰當的使用這些新特性在某些場景下的確可以取得很好的效果(簡潔的代碼,優秀的性能)。這篇文章的初衷一是對自己所得的記錄,二是做一個分享。寫得不好的或者謬誤的地方還請大家批評指正,一起交流,共同進步。

參考文獻
Lambda演算-wikipedia
JSR335-FINAL(Java Specification Requests)
Oracle-Java SE 8: Lambda Quick Start

My Github


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM