Java Lambda 表达式源码分析,那些年Java面试官常问的知识点

基本概念


Lambda 表达式

下面的例子中, () -> System.out.println("1") 就是一个Lambda 表达式。Java 8 中每一个Lambda 表达式必须有一个函数式接口与之对应。Lambda表达式就是函数式接口的一个实现。

@Test

public void test0() {

Runnable runnable = () -> System.out.println(“1”);

runnable.run();

ToIntBiFunction<Integer, Integer> function = (n1, n2) -> n1 + n2;

System.out.println(function.applyAsInt(1, 2));

ToIntBiFunction<Integer, Integer> function2 = Integer::sum;

System.out.println(function2.applyAsInt(1, 2));

}

大致形式就是 (param1, param2, param3, param4…) -> { doing…… };

函数式接口


首先要从 FunctionalInterface 注解讲起,详情见 Annotation Type FunctionalInterface 。

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

简单总结一下函数式接口的特征:

  • FunctionalInterface 注解标注一个函数式接口,不能标注类,方法,枚举,属性这些。

  • 如果接口被标注了 @FunctionalInterface,这个类就必须符合函数式接口的规范。

  • 即使一个接口没有标注 @FunctionalInterface,如果这个接口满足函数式接口规则,依旧可以被当作函数式接口。

注意:interface 中重写 Object 类中的抽象方法,不会增加接口的方法数,因为接口的实现类都是 Object 的子类。

我们可以看到 Runnable 接口,里面只有一个抽象方法 run() ,则这个接口就是一个函数式接口。

@FunctionalInterface

public interface Runnable {

public abstract void run();

}

方法引用


所谓方法引用,是指如果某个方法签名和接口恰好一致,就可以直接传入方法引用。文章开头的示例中,下面这块代码就是方法引用。

ToIntBiFunction<Integer, Integer> function2 = Integer::sum;

java.lang.Integer#sum 的实现如下:

public static int sum(int a, int b) {

return a + b;

}

比如我们计算一个 Stream 的和,可以直接传入 Integer::sum 这个方法引用。

@Test

public void test1() {

Integer sum = IntStream.range(0, 10).boxed().reduce(Integer::sum).get();

System.out.println(sum);

}

上面的代码中,为什么可以直接在 reduce 方法中传入 Integer::sum 这个方法引用呢?这是因为 reduce 方法的入参就是 BinaryOperator 的函数式接口。

Optional reduce(BinaryOperator accumulator);

BinaryOperator 是继承自 BiFunction ,定义如下:

@FunctionalInterface

public interface BiFunction<T, U, R> {

R apply(T t, U u);

default BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {

Objects.requireNonNull(after);

return (T t, U u) -> after.apply(apply(t, u));

}

}

可以看到,只要是符合R apply(T t, U u); 的方法引用,都可以传入 reduce 中。可以是上面代码中的 Integer::sum ,也可以是 Integer::max

深入实现原理


字节码

首先写 2 个 Lambda 方法:

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

820192088)]
[外链图片转存中…(img-ijAwN4ow-1710820192089)]
[外链图片转存中…(img-w6OoMnu3-1710820192089)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-2knzIsev-1710820192089)]

  • 30
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值