學習Java8系列-Lambda

大魔王先生發表於2020-07-14

Lambda演進

小王在公司正在開發一個學生管理系統,產品經理向他提出一個需求,要篩選出年齡大於15的學生,於是小王寫出了以下程式碼:

    public static List<Student> filterAgeStudent(List<Student> students) {
        List<Student> list = Lists.newArrayList();
        for (Student student : students) {
            if (student.getAge() > 15) {
                list.add(student);
            }
        }
        return list;
    }

過了幾天產品經理又提出了一個需求,要篩選出體重大於50KG的學生,於是小王新增了一個方法:

    public static List<Student> filterWeightStudent(List<Student> students) {
        List<Student> list = Lists.newArrayList();
        for (Student student : students) {
            if (student.getWeight() > 50) {
                list.add(student);
            }
        }
        return list;
    }

過了一段時間,產品提出了要篩選出體重大於50並且年齡要大於15的學生,小王突然感覺到這不是一個簡單的需求,於是小王仔細思考了一下,突然想到將每種篩選的策略抽象成為一個介面,並且將這個介面當做一個引數傳入方法中,這樣每次就可以只新增策略,其他程式碼不需要更改了,這樣就滿足了軟體設計的六大原則的開放閉合原則,於是乎誕生以下的設計和程式碼:

public interface StudentPredicate {
    boolean filter(Student student);
}
public class AgeStudentPredicate implements StudentPredicate {
    @Override
    public boolean filter(Student student) {
        return student.getAge() > 20 ? true : false;
    }
}
public static List<Student> filterStudent(List<Student> students,
                                              StudentPredicate predicate)
 
{
    List<Student> list = Lists.newArrayList();
    for (Student student : students) {
       if (predicate.filter(student)) {
          list.add(student);
       }
    }
    return list;
}

經過一段時間的學習,小王接觸到匿名類,於是小王程式碼進行更改,以後再也不需要寫策略了:

List<Student> list = filterStudent(students, new StudentPredicate() {
     @Override
     public boolean filter(Student student) {
         return student.getAge() > 15;
     }
});

學習到匿名類以後,小王感覺到Java的浩瀚,然後繼續學習,後來接觸到Lambda,於是對待做了以下改造:

 List<Student> list = filterStudent(students, student -> student.getAge() > 15);

Lambda知識整理

Lambda定義

從上面的演進過程,我們基本上可以得到Lambda表示式是一種匿名函式,簡單地說,它是沒有宣告的方法,也即沒有訪問修飾符、返回值宣告和名字。Java中的Lambda表示式通常使用(argument) -> (body)語法書寫,常用的Lamda表示式例子有:

(int a, int b) -> {  return a + b; }

() -> System.out.println("Hello World");

(String s) -> { System.out.println(s); }
函式式介面

函式式介面指的是是隻包含一個抽象方法宣告的介面。例如java.lang.Runnable就是一種函式式介面,在 Runnable介面中只宣告瞭一個抽象方法方法void run();

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}

對於註解@FunctionalInterface對於lamda表示式來說的話並不是必要的,@FunctionalInterface是Java8新加入的一種介面,用於指明該介面型別宣告是根據Java語言規範定義的函式式介面。Java8還宣告瞭一些Lambda表示式可以使用的函式式介面,當你註釋的介面不是有效的函式式介面時,可以使用@FunctionalInterface解決編譯層面的錯誤。

常用函式式

Java8中在java.util.function中引入了很多的函式式介面,這裡介紹一下3個常用的函式式介面,

  1. Predicate
    Predicate介面定義一個名叫test的抽象方法,它接收泛型T物件,並返回一個boolean型別。經常使用到的地方是在流處理的過程中filter方法,滿足條件的資料才會被過濾出來,例如我們上面的例子也可以改造成為Predicate函式式介面的形式。
@FunctionalInterface
public interface Predicate<T{
   boolean test(T t);
}
public static List<Student> filterStudent(List<Student> students,
                                          Predicate<Student> predicate) {
   List<Student> list = Lists.newArrayList();
   for (Student student : students) {
   if (predicate.test(student)) {
     list.add(student);
   }
  }
  return list;
}
  1. Consumer
    Consumer定義一個名叫accept的抽象方法,他接受泛型T的物件,沒有返回值。如果你需要訪問泛型物件T,並其進行修改,就使用Consumer。經常使用的地方就是常用的forEach方法。
@FunctionalInterface
public interface Consumer<T{
  void accept(T t);
}
void forEachOrdered(Consumer<? super T> action);
  1. Function
    Function定義一個叫apply的方法,他接受一個泛型物件T,返回一個泛型物件R。如果你需要定義一個Lambda表示式,將輸入的物件對映到輸出,就使用Function,經常使用到的地方就是常用的map方法。
@FunctionalInterface
public interface Function<TR{
  apply(T t);
}
<R> Stream<R> map(Function<? super T, ? extends R> mapper);

Lambda原理窺探

小王經過上面一系列學習,開始思考Lambda的原理是什麼,因為Java8中每一個Lambda表示式必須有一個函式式介面與之對應,小王就思考經過編譯器編譯以後到可能實現的方式有兩種,一種生成實現介面的類,另外一種是內部類,於是決定看一下反編譯的以後程式碼,以解除心中的疑惑;

@FunctionalInterface
public interface Func {
    int add(int x, int y);
}
public class LambdaTest {
    public static void main(String[] args{
        Func func = (x, y) -> x + y;
        System.out.println(func.add(12));
    }
}

通過javap -p -v -c LambdaTest.class檢視反編譯後的程式碼,

Classfile /Users/wangtongzhou/Documents/Java/learning/target/classes/com/springboot2/learning/javabasic/java8/LambdaTest.class
  Last modified 2020-7-11; size 1392 bytes
  MD5 checksum ec7d77a8b0b0a0cb5940f80a9b27b3d0
  Compiled from "LambdaTest.java"
public class com.springboot2.learning.javabasic.java8.LambdaTest
  minor version0
  major version52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #7.#29         // java/lang/Object."<init>":()V
   #2 = InvokeDynamic      #0:#34         // #0:add:()Lcom/springboot2/learning/javabasic/java8/Func;
   #3 = Fieldref           #35.#36        // java/lang/System.out:Ljava/io/PrintStream;
   #4 = InterfaceMethodref #37.#38        // com/springboot2/learning/javabasic/java8/Func.add:(II)I
   #5 = Methodref          #39.#40        // java/io/PrintStream.println:(I)V
   #6 = Class              #41            // com/springboot2/learning/javabasic/java8/LambdaTest
   #7 = Class              #42            // java/lang/Object
   #8 = Utf8               <init>
   #9 = Utf8               ()V
  #10 = Utf8               Code
  #11 = Utf8               LineNumberTable
  #12 = Utf8               LocalVariableTable
  #13 = Utf8               this
  #14 = Utf8               Lcom/springboot2/learning/javabasic/java8/LambdaTest;
  #15 = Utf8               main
  #16 = Utf8               ([Ljava/lang/String;)V
  #17 = Utf8               args
  #18 = Utf8               [Ljava/lang/String;
  #19 = Utf8               func
  #20 = Utf8               Lcom/springboot2/learning/javabasic/java8/Func;
  #21 = Utf8               MethodParameters
  #22 = Utf8               lambda$main$0
  #23 = Utf8               (II)I
  #24 = Utf8               x
  #25 = Utf8               I
  #26 = Utf8               y
  #27 = Utf8               SourceFile
  #28 = Utf8               LambdaTest.java
  #29 = NameAndType        #8:#9          // "<init>":()V
  #30 = Utf8               BootstrapMethods
  #31 = MethodHandle       #6:#43         // invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
  #32 = MethodType         #23            //  (II)I
  #33 = MethodHandle       #6:#44         // invokestatic com/springboot2/learning/javabasic/java8/LambdaTest.lambda$main$0:(II)I
  #34 = NameAndType        #45:#46        // add:()Lcom/springboot2/learning/javabasic/java8/Func;
  #35 = Class              #47            // java/lang/System
  #36 = NameAndType        #48:#49        // out:Ljava/io/PrintStream;
  #37 = Class              #50            // com/springboot2/learning/javabasic/java8/Func
  #38 = NameAndType        #45:#23        // add:(II)I
  #39 = Class              #51            // java/io/PrintStream
  #40 = NameAndType        #52:#53        // println:(I)V
  #41 = Utf8               com/springboot2/learning/javabasic/java8/LambdaTest
  #42 = Utf8               java/lang/Object
  #43 = Methodref          #54.#55        // java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
  #44 = Methodref          #6.#56         // com/springboot2/learning/javabasic/java8/LambdaTest.lambda$main$0:(II)I
  #45 = Utf8               add
  #46 = Utf8               ()Lcom/springboot2/learning/javabasic/java8/Func;
  #47 = Utf8               java/lang/System
  #48 = Utf8               out
  #49 = Utf8               Ljava/io/PrintStream;
  #50 = Utf8               com/springboot2/learning/javabasic/java8/Func
  #51 = Utf8               java/io/PrintStream
  #52 = Utf8               println
  #53 = Utf8               (I)V
  #54 = Class              #57            // java/lang/invoke/LambdaMetafactory
  #55 = NameAndType        #58:#62        // metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
  #56 = NameAndType        #22:#23        // lambda$main$0:(II)I
  #57 = Utf8               java/lang/invoke/LambdaMetafactory
  #58 = Utf8               metafactory
  #59 = Class              #64            // java/lang/invoke/MethodHandles$Lookup
  #60 = Utf8               Lookup
  #61 = Utf8               InnerClasses
  #62 = Utf8               (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
  #63 = Class              #65            // java/lang/invoke/MethodHandles
  #64 = Utf8               java/lang/invoke/MethodHandles$Lookup
  #65 = Utf8               java/lang/invoke/MethodHandles
{
  public com.springboot2.learning.javabasic.java8.LambdaTest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/springboot2/learning/javabasic/java8/LambdaTest;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=1
         0: invokedynamic #2,  0              // InvokeDynamic #0:add:()Lcom/springboot2/learning/javabasic/java8/Func;
         5: astore_1
         6: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         9: aload_1
        10: iconst_1
        11: iconst_2
        12: invokeinterface #4,  3            // InterfaceMethod com/springboot2/learning/javabasic/java8/Func.add:(II)I
        17: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        20: return
      LineNumberTable:
        line 5: 0
        line 6: 6
        line 7: 20
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      21     0  args   [Ljava/lang/String;
            6      15     1  func   Lcom/springboot2/learning/javabasic/java8/Func;
    MethodParameters:
      Name                           Flags
      args

  private static int lambda$main$0(int, int);
    descriptor: (II)I
    flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=2, locals=2, args_size=2
         0: iload_0
         1: iload_1
         2: iadd
         3: ireturn
      LineNumberTable:
        line 5: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       4     0     x   I
            0       4     1     y   I
    MethodParameters:
      Name                           Flags
      x                              synthetic
      y                              synthetic
}
SourceFile: "LambdaTest.java"
InnerClasses:
     public static final #60= #59 of #63; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
BootstrapMethods:
  0#31 invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
    Method arguments:
      #32 (II)I
      #33 invokestatic com/springboot2/learning/javabasic/java8/LambdaTest.lambda$main$0:(II)I

反編譯以後lambda表示式被編譯成為一個lambda$main$0的函式,其實就是一段(x, y) -> x + y的方法,在看main方法主要分為以下8個步驟:

  1. 通過invokedynamic指令生成呼叫物件;
  2. 存入本地快取;
  3. 載入java.lang.System.out靜態方法;
  4. 將lambda表示式生成的物件載入入執行棧;
  5. 將int型別1載入入執行棧;
  6. 將int型別2載入入執行棧;
  7. 執行lambda表示式生成的物件的add方法;
  8. 輸出執行結果;
    重點部分重點部分
    從mian方法中我們的重點就在於invokedynamic這個指令,重點要了解下是如何通過invokedynamic指令生成目標物件,invokedynamic指令通過找到BootstrapMethods中的方法,生成動態呼叫點,也是呼叫LambdaMetafactory的metafactory方法。
public static CallSite metafactory(MethodHandles.Lookup caller,
                                       String invokedName,
                                       MethodType invokedType,
                                       MethodType samMethodType,
                                       MethodHandle implMethod,
                                       MethodType instantiatedMethodType)

            throws LambdaConversionException 
{
        AbstractValidatingLambdaMetafactory mf;
        mf = new InnerClassLambdaMetafactory(caller, invokedType,
                                             invokedName, samMethodType,
                                             implMethod, instantiatedMethodType,
                                             false, EMPTY_CLASS_ARRAY, 
                                             EMPTY_MT_ARRAY);
        mf.validateMetafactoryArgs();
        return mf.buildCallSite();
}

通過原始碼可以看出,metafactory方法通過InnerClassLambdaMetafactory類生成物件,並提供後續呼叫,在InnerClassLambdaMetafactory原始碼中可以看到,有提供開關是否dump生成的class檔案。



接下來我們通過設定啟動引數-Djdk.internal.lambda.dumpProxyClasses檢視中間物件,增加這個引數以後會生成LambdaTest$$Lambda$1類,
final class LambdaTest$$Lambda$1 implements Func {
    private LambdaTest$$Lambda$1() {
    }

    @Hidden
    public int add(int var1, int var2) {
        return LambdaTest.lambda$main$0(var1, var2);
    }
}

我們再看下上面這個類反編譯以後的情況

Classfile /Users/wangtongzhou/Documents/Java/learning/com/springboot2/learning/javabasic/java8/LambdaTest$$Lambda$1.class
  Last modified 2020-7-11; size 437 bytes
  MD5 checksum 729979930540708c60f4e71e63b69321
final class com.springboot2.learning.javabasic.java8.LambdaTest$$Lambda$1 implements com.springboot2.learning.javabasic.java8.Func
  minor version: 0
  major version: 52
  flags: ACC_FINAL, ACC_SUPER, ACC_SYNTHETIC
Constant pool:
   #1 = Utf8               com/springboot2/learning/javabasic/java8/LambdaTest$$Lambda$1
   #2 = Class              #1             // com/springboot2/learning/javabasic/java8/LambdaTest$$Lambda$1
   #3 = Utf8               java/lang/Object
   #4 = Class              #3             // java/lang/Object
   #5 = Utf8               com/springboot2/learning/javabasic/java8/Func
   #6 = Class              #5             // com/springboot2/learning/javabasic/java8/Func
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = NameAndType        #7:#8          // "<init>":()V
  #10 = Methodref          #4.#9          // java/lang/Object."<init>":()V
  #11 = Utf8               add
  #12 = Utf8               (II)I
  #13 = Utf8               Ljava/lang/invoke/LambdaForm$Hidden;
  #14 = Utf8               com/springboot2/learning/javabasic/java8/LambdaTest
  #15 = Class              #14            // com/springboot2/learning/javabasic/java8/LambdaTest
  #16 = Utf8               lambda$main$0
  #17 = NameAndType        #16:#12        // lambda$main$0:(II)I
  #18 = Methodref          #15.#17        // com/springboot2/learning/javabasic/java8/LambdaTest.lambda$main$0:(II)I
  #19 = Utf8               Code
  #20 = Utf8               RuntimeVisibleAnnotations
{
  private com.springboot2.learning.javabasic.java8.LambdaTest$$Lambda$1();
    descriptor: ()V
    flags: ACC_PRIVATE
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #10                 // Method java/lang/Object."<init>":()V
         4return

  public int add(intint);
    descriptor: (II)I
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
         0: iload_1
         1: iload_2
         2: invokestatic  #18                 // Method com/springboot2/learning/javabasic/java8/LambdaTest.lambda$main$0:(II)I
         5: ireturn
    RuntimeVisibleAnnotations:
      0#13()
}

由此我們可以得出編譯以後的程式碼為:

public class LambdaTest {
    public static void main(String[] args) {
       Func func= LambdaTest$$Lambda$1();
       System.out.println(func.add(12));
    }
    private static int lambda$main$0(int x, int y) {
        return x + y;
    }

    static final class LambdaTest$$Lambda$1 implements Func {
    private LambdaTest$$Lambda$1() {
    }

    public int add(int x, inty) {
        return LambdaTest.lambda$main$0(x,y);
    }
  }
}

總結下,Lambda底層就是通過一個靜態的內部類實現的;

結尾

歡迎大家點點關注,點點贊,感謝!

相關文章