Lambda表达式一(Java)

什么是Lambda表达式?

JAVA8新特性,是一种匿名函数。它能够使代码变得简洁、紧凑,Lambda表达式实质上是个语法糖。

使用Lambda的前提

必须有一个函数式接口 有且只有一个抽象方法的接口 @FunctionnalInterface注解(可选)

常见的函数式接口

Runnable

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

Callable

@FunctionalInterface
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

Supplier

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

Consumer

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

}

Comparator

@FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
}

Predicate

@FunctionalInterface
public interface Predicate<T> {
    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

Function

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
}

Lambda表达式的语法格式

基本语法

具体案例 

1.可选的大括号{}

函数体只包含一个语句,不需要大括号

// 1.函数体只有一个语句,省略大括号
(String msg) -> System.out.println("hello"+ msg);

2.可选的参数圆括号()

只有一个参数,省略圆括号,同时省略类型

// 2.只有一个参数,省略圆括号,同时省略类型
msg -> System.out.println("hello “ + msg);

3.可选的返回关键字return

函数体只有一个表达式,且运算结果匹配返回类型

// 3.函数体只有一个表达式,省略return
(int a, int b) -> a + b

4.可选的类型声明

不需要参数类型,编译器可以根据参数值进行推断

/ 4. 省略参数类型,编译器可以进行推断
(a, b) -> a + b

Lambda的底层实现?

它的本质是函数式接口的匿名子类的匿名对象。Lambda底层用匿名内部类实现。通过反编译可以看出

java -jar cfr-0.145.jar LambdaTest.class --decodelambdas false

源代码:

public class LambdaTest {
    public static void main(String[] args) {
        List<String> strList = Arrays.asList("xiao", "wang");
        // 通过lambda表达式实现元素遍历
        strList.forEach(s -> {
            System.out.println(s);
        });
    }
}

 cfr-0.145.jar包: 

链接:https://pan.baidu.com/s/1HWoK8_Fy-YTRiXk8UitYRQ 
提取码:1234 
--来自百度网盘超级会员V5的分享
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半路出家的码农小王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值