Java8 Lambda表达式,常用接口,Stream流式处理
java8 stream 源码分析
java8 lambda底层原理
.
Java规范请求 — JSR 292:在Java™平台上支持动态类型语言
JVM规范— invokedynamic 指令
Java stream 知识点整理
闭包(Closure)
闭包就是函数对象与外界变量绑定在一起,形成的整体。
1 | public class ClosureTest1 { |
柯里化(Carrying)
第一次知道柯里化,还是在这里 》》》 Lambda函数的柯里化 — 2022-07-16 18:48:07
柯里化的作用是让函数对象分步执行(本质上是利用多个函数对象和闭包)
1 | public class Carrying1Test { |
优化效率
1 | 计算 => IntStream、LongStream、。。。 效率接近原生 for循环 |
MyBatis-Plus Student::getName()
1 | public class MyTest { |
lambda原理
lambda本质
1 | public class MyRunnable1 { |
当时学 lambda 的时候,一直认为二者是相同的,最近才发现,完成不是的。(r2 会动态生成类和方法,而 r1 则不会)。
下面来一步步分析。
0、先了解两个知识点
1 | # 1、javap 命令 |
1、先来分析 r1
1 | 会生成一个 class文件,表示内部类, 真正执行的时候,会创建内部类 impl Runnable,调用内部类.run()。 |
2、再来分析 r2
1 | 1、当前类中生成方法,记作方法 a(只在内存中,磁盘中的class信息中不会有这个方法。 此方法用于 lambda类调用。) |
1 | import java.lang.invoke.CallSite; |
BiFunction 有参数和返回值
上面 Runnable 是无参、无返回值的,这里再来看一个有参、有返回值的。
1 | import java.lang.invoke.CallSite; |
Student::getName 方法引用
1 | import java.lang.invoke.CallSite; |
闭包 局部变量
1 | import java.lang.reflect.Method; |
闭包 静态变量
1 | import java.lang.reflect.Method; |
闭包 成员变量
这里不同的是,lambda 对象也是成员变量,生成的方法不再是 static 静态方法。
1 | import java.lang.reflect.Method; |