JDK1.8新特性(Lambda表达式)

为什么使用Lambda表达式

Lambda是一个匿名函数,我们可以吧Lambda表达式理解为一段可以传递代码(将代码像数据一样进行传递)。可以写出更加简单、更灵活的代码。作为一种更紧凑的代码风格,是Java的语言表达能力得到了提升。

语法

Lambda表达式的基础语法:Java8中引入一个新的操作符"->",该操作符称为箭头操作符或者Lambda操作符,箭头操作符拆分为两部分,左侧和右侧,左侧:Lambda 表达式的参数列表;右侧:Lambda 表达式中需要执行的功能逻辑,即Lambda体
注意:Lambda表达式需要函数式接口支持,函数是接口(接口中只有一个抽象的接口,称为函数式接口。

语法一:无参数,无返回值
() -> System.out.println(x);

	@Test
    public void test1(){
        Runnable r=new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello World");
            }
        };
        r.run();;

        System.out.println("----------------------------------");

        Runnable rr=()-> System.out.print("Hello Lambda");
        rr.run();
    }

语法二:有一个参数,无返回值
(x) -> System.out.println(x);

	@Test
    public void test2(){
        Consumer<String> con= (x) -> System.out.print(x);
        con.accept("测试");
    }

语法三:有一个参数,无返回值
x -> System.out.println(x);

	@Test
    public void test3(){
        Consumer<String> con= x -> System.out.print(x);
        con.accept("测试");
    }

语法四:有两个以上的参数,有返回值,并且Lambda 体中有多条语句,{}括起来

	@Test
    public void test4(){
        Comparator<Integer> com=(x,y) ->{
            System.out.println("函数式接口");
            return Integer.compare(x, y);
        };
    }

语法五:有两个以上的参数,有返回值,并且Lambda 中只有一条语句,{}可以省略,return可以省略

	@Test
    public void test5(){
        Comparator<Integer> com=(x,y) ->Integer.compare(x, y);
    }

语法六:Lambda表达式的参数列表的数据类型可以忽略不写,因为JVM编译器通过上下文推断出,数据类型,即"类型判断"(Integer x,Integer y) -> Integer.compare(x,y);

	@Test
    public void test6(){
        Comparator<Integer> com=(Integer x,Integer y)  ->Integer.compare(x, y);
    }

函数式接口编写

函数接口

/**
* @description: FunctionalInterface标注为函数接口
* @author TAO
* @date 2021/3/28 16:37
*/
@FunctionalInterface
public interface MyFun {
    public Integer getValue(Integer num);
}

对一个数进行任意运算

	//对一个数进行任意运算
    @Test
    public void test7(){
        Integer num = operation(100, (x) -> x * x);
        System.out.println(num);

        Integer num2 = operation(100, (x) -> x + x);
        System.out.println(num2);
    }

    public Integer operation(Integer num,MyFun myFun){
        return myFun.getValue(num);
    }

函数接口

/**
* @description: FunctionalInterface标注为函数接口
* @author TAO
* @date 2021/3/28 16:37
*/
@FunctionalInterface
public interface MyFun2 {
    public String getValue(String string);
}

对一个字符串进行任意处理

	//对一个字符串进行任意处理
    @Test
    public void test8(){
        String str1 = strHandler("\t\t\t TAO", (str) -> str.trim());
        System.out.println(str1);

        String str2 = strHandler("abghfdsfdsf", (str) -> str.toUpperCase());
        System.out.println(str2);

        String str3 = strHandler("HelloWorld", (str) -> str.substring(1,3));
        System.out.println(str3);
    }

    public String strHandler(String str,MyFun2 myFun2){
        return myFun2.getValue(str);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员劝退师-TAO

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

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

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

打赏作者

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

抵扣说明:

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

余额充值