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);
    }

猜你喜欢

转载自blog.csdn.net/CSDN877425287/article/details/115270731