天天看點

深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

一、背景

Java 8 的 Lambda 表達式已經不再是“新特性”。

現在很多人工作中會使用 Lambda 表達式。

但是,你是否真正了解 Lambda 表達式的底層原理?

深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

本文給出自己的了解,希望對大家有幫助。

二、分析

下面是一段非常簡單的代碼,其中用到了

Stream

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ListDemo {
    public static void main(String[] args) {
        List<DogDO> dogs = new ArrayList<>();

        List<String> tom = dogs.stream().filter(dog -> dog.getName().startsWith("tom")).map(dog -> dog.getName().toLowerCase()).collect(Collectors.toList());
        System.out.println(tom);
    }
}           

我們使用

Jclasslib

插件(

《那些相見恨晚的 IDEA插件》

中有介紹),檢視位元組碼:

深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

大家也可以自己使用 javac 和 javap 指令去在指令行執行。

如:

javac ListDemo.java
 javap -p -s -c -v -l ListDemo           

我們可以看到多出一個内部類和 BootstrapMethods

lambda$main$0

0 aload_0
1 ldc #20 <tom>
3 invokevirtual #21 <java/lang/String.startsWith : (Ljava/lang/String;)Z>
6 ireturn

           
深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

相當于:

private static boolean lambda$main$0(String name){
   return name.startsWith("tom");
}           

lambda$main$1

0 aload_0
1 invokevirtual #19 <java/lang/String.toLowerCase : ()Ljava/lang/String;>
4 areturn

           
深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

相當于

private static String lambda$main$1(String name){
   return name.toLowerCase();
}           

通過上述簡單分析就可以看出來,本質上 lambda 表達式最終會被編譯為私有靜态方法。

main 方法

0 new #2 <java/util/ArrayList>
 3 dup
 4 invokespecial #3 <java/util/ArrayList.<init> : ()V>
 7 astore_1
 8 aload_1
 9 ldc #4 <jam>
11 invokeinterface #5 <java/util/List.add : (Ljava/lang/Object;)Z> count 2
16 pop
17 aload_1
18 ldc #6 <tom cat>
20 invokeinterface #5 <java/util/List.add : (Ljava/lang/Object;)Z> count 2
25 pop
26 aload_1
27 ldc #7 <tom jetty>
29 invokeinterface #5 <java/util/List.add : (Ljava/lang/Object;)Z> count 2
34 pop
35 aload_1
36 ldc #8 <gom jetty>
38 invokeinterface #5 <java/util/List.add : (Ljava/lang/Object;)Z> count 2
43 pop
44 aload_1
45 invokeinterface #9 <java/util/List.stream : ()Ljava/util/stream/Stream;> count 1
50 invokedynamic #10 <test, BootstrapMethods #0>
55 invokeinterface #11 <java/util/stream/Stream.filter : (Ljava/util/function/Predicate;)Ljava/util/stream/Stream;> count 2
60 invokedynamic #12 <apply, BootstrapMethods #1>
65 invokeinterface #13 <java/util/stream/Stream.map : (Ljava/util/function/Function;)Ljava/util/stream/Stream;> count 2
70 invokestatic #14 <java/util/stream/Collectors.toList : ()Ljava/util/stream/Collector;>
73 invokeinterface #15 <java/util/stream/Stream.collect : (Ljava/util/stream/Collector;)Ljava/lang/Object;> count 2
78 checkcast #16 <java/util/List>
81 astore_2
82 getstatic #17 <java/lang/System.out : Ljava/io/PrintStream;>
85 aload_2
86 invokevirtual #18 <java/io/PrintStream.println : (Ljava/lang/Object;)V>
89 return
           

通過

invokedynamic

指令執行動态方法調用。

50 invokedynamic #10 <test, BootstrapMethods #0>           

我們可以直接在插件上,點選指令跳轉到對應官方說明文檔中:

https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-6.html#jvms-6.5.invokedynamic
深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

從 JVM 文檔 可以看到 該方法是為了實作動态調用計算,它通過常量池重點額符号引用轉為動态計算調用,其中 CallSite 執行個體就是目标方法調用執行個體。

可以在插件裡一直跟下去

invokedynamic #10 中的常量池中的 #10 為下面的内容:

深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

其中

BootstrapMethods # 0

對應

深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

可以看到這是對

java.lang.invoke.LambdaMetafactory#metafactory

的調用,傳回值是 java.lang.invoke.CallSite 對象,這個對象代表了真正執行的目标方法調用。

/**
     * Facilitates the creation of simple "function objects" that implement one
     * or more interfaces by delegation to a provided {@link MethodHandle},
     * after appropriate type adaptation and partial evaluation of arguments.
     * Typically used as a <em>bootstrap method</em> for {@code invokedynamic}
     * call sites, to support the <em>lambda expression</em> and <em>method
     * reference expression</em> features of the Java Programming Language.
     *
     * <p>This is the standard, streamlined metafactory; additional flexibility
     * is provided by {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}.
     * A general description of the behavior of this method is provided
     * {@link LambdaMetafactory above}.
     *
     * <p>When the target of the {@code CallSite} returned from this method is
     * invoked, the resulting function objects are instances of a class which
     * implements the interface named by the return type of {@code invokedType},
     * declares a method with the name given by {@code invokedName} and the
     * signature given by {@code samMethodType}.  It may also override additional
     * methods from {@code Object}.
     *
     * @param caller Represents a lookup context with the accessibility
     *               privileges of the caller.  Specifically, the lookup context
     *               must have
     *               <a href="MethodHandles.Lookup.html#privacc">private access</a>
     *               privileges.
     *               When used with {@code invokedynamic}, this is stacked
     *               automatically by the VM.
     * @param invokedName The name of the method to implement.  When used with
     *                    {@code invokedynamic}, this is provided by the
     *                    {@code NameAndType} of the {@code InvokeDynamic}
     *                    structure and is stacked automatically by the VM.
     * @param invokedType The expected signature of the {@code CallSite}.  The
     *                    parameter types represent the types of capture variables;
     *                    the return type is the interface to implement.   When
     *                    used with {@code invokedynamic}, this is provided by
     *                    the {@code NameAndType} of the {@code InvokeDynamic}
     *                    structure and is stacked automatically by the VM.
     *                    In the event that the implementation method is an
     *                    instance method and this signature has any parameters,
     *                    the first parameter in the invocation signature must
     *                    correspond to the receiver.
     * @param samMethodType Signature and return type of method to be implemented
     *                      by the function object.
     * @param implMethod A direct method handle describing the implementation
     *                   method which should be called (with suitable adaptation
     *                   of argument types, return types, and with captured
     *                   arguments prepended to the invocation arguments) at
     *                   invocation time.
     * @param instantiatedMethodType The signature and return type that should
     *                               be enforced dynamically at invocation time.
     *                               This may be the same as {@code samMethodType},
     *                               or may be a specialization of it.
     * @return a CallSite whose target can be used to perform capture, generating
     *         instances of the interface named by {@code invokedType}
     * @throws LambdaConversionException If any of the linkage invariants
     *                                   described {@link LambdaMetafactory above}
     *                                   are violated, or the lookup context
     *                                   does not have private access privileges.
     */
    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();
    }           

即 lambda 表達式将代碼寫到私有靜态方法中,然後構造目标類型的實作。

為了更直覺地看到效果,大家可以在 IDEA 該類運作參數上加上

-Djdk.internal.lambda.dumpProxyClasses=你電腦上的想輸出到的路徑
深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

可以看到編譯出來的内部類:

深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

可以使用

Luyten

反編譯工具。

下載下傳位址在這裡:

深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

檢視源碼:

深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結
深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結

可以看到編譯器自動幫我們生成了

Predicate

Function

内部類,類名為

[目标Class$$Lambda$數字]

的形式,在内部類中調用上面的靜态方法。

邏輯層面等價于下面代碼:

package other.list;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class ListDemo {
    public static void main(String[] args) {
        List<String> dogNames = new ArrayList<>();
        dogNames.add("jam");
        dogNames.add("tom cat");
        dogNames.add("tom jetty");
        dogNames.add("gom jetty");


        List<String> tom = dogNames.stream().filter(new ListDemo$$Lambda$1()).map(new ListDemo$$Lambda$2()).collect(Collectors.toList());
        System.out.println(tom);
    }

    private static boolean lambda$min$0(String name) {
        return name.startsWith("tom");
    }

    private static String lambda$main$1(String name) {
        return name.toLowerCase();
    }

    static class ListDemo$$Lambda$1 implements Predicate<String> {
        @Override
        public boolean test(String name) {
            return lambda$min$0(name);
        }
    }

    static class ListDemo$$Lambda$2 implements Function<String, String> {
        @Override
        public String apply(String name) {
            return lambda$main$1(name);
        }
    }
}



           

三、拓展

有了上面的講解,相信大家對 Lambda 表達式已經有了較深的了解。

請大家猜想并動手驗證 Map.forEach 寫法的底層實作是怎樣的?

示例:

import java.util.HashMap;
import java.util.Map;

public class LambdaMapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put(i, String.valueOf(i));
        }

        // 底層如何實作?
        map.forEach((k, v) -> {
            System.out.println("k:" + k + " -> v:" + v);
        });
    }
}
           

相信大家看到下面 main 函數的位元組碼,應該已經可以腦補出實作方式:

0 new #2 <java/util/HashMap>
 3 dup
 4 invokespecial #3 <java/util/HashMap.<init> : ()V>
 7 astore_1
 8 iconst_0
 9 istore_2
10 iload_2
11 bipush 10
13 if_icmpge 37 (+24)
16 aload_1
17 iload_2
18 invokestatic #4 <java/lang/Integer.valueOf : (I)Ljava/lang/Integer;>
21 iload_2
22 invokestatic #5 <java/lang/String.valueOf : (I)Ljava/lang/String;>
25 invokeinterface #6 <java/util/Map.put : (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;> count 3
30 pop
31 iinc 2 by 1
34 goto 10 (-24)
37 aload_1
38 invokedynamic #7 <accept, BootstrapMethods #0>
43 invokeinterface #8 <java/util/Map.forEach : (Ljava/util/function/BiConsumer;)V> count 2
48 return           

請大家自己寫一下邏輯上的等價代碼。

希望大家可以有看到 Lambda 表達式,就可以自行腦補出底層實作的能力。

四、總結

很多知識看似習以為常,但是都可以繼續深挖,可以學到不一樣的知識。

隻有真正搞透一個知識點面試中才會有足夠的自信。

另外 Lambda 雖好,但不要“貪杯”,濫用 Lambda 對代碼的可讀性和可維護性都會到來挑戰。

創作不易,如果本文對你有幫助,歡迎點贊、收藏加關注,你的支援和鼓勵,是我創作的最大動力。
深入了解Java Lambda 表達式一、背景二、分析三、拓展四、總結