编程语言
首页 > 编程语言> > Java8-lambda表达式

Java8-lambda表达式

作者:互联网

Lambda 表达式的使用

1.举例:

(o1, o2) -> Integer.compare(o1, o2)

2.格式:

3.Lambda表达式的使用:(分为6种情况)

4.Lambda的实质:作为接口的实例

总结

代码

public class LambdaTest1 {

    //语法格式一: 无参,无返回值
    @Test
    public void test1(){

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("测试一");
            }
        };
        r1.run();
        System.out.println("********************");
        Runnable r2 = () -> {
            System.out.println("测试一");
        };
        r2.run();
    }
    //语法规则二: Lambda表达式需要一个参数,但没有返回值
    @Test
    public void test2(){
        Consumer<String> con1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        Consumer<String> con2 = (String s) -> {
            System.out.println(s);
        };
        con1.accept("测试二");
        System.out.println("********************");
        con2.accept("测试二");
    }
    //语法规则三: 数据类型可以胜率,可由编译器推断,称为“类型推断”
    @Test
    public void test3(){
        Consumer<String> con1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        Consumer<String> con2 = (s) -> {
            System.out.println(s);
        };
        con1.accept("测试二");
        System.out.println("********************");
        con2.accept("测试二");
    }
    //语法规则四: 只有一个参数,小括号可以省略
    @Test
    public void test4(){
        Consumer<String> con1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        Consumer<String> con2 = s -> {
            System.out.println(s);
        };
        con1.accept("测试二");
        System.out.println("********************");
        con2.accept("测试二");
    }
    //语法规则五: 有两个或多个参数,有多条语句,并可以有返回值
    @Test
    public void test5(){

        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return o1.compareTo(o2);
            }
        };
        Comparator<Integer> com2 = (o1,o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };

        System.out.println(com1.compare(10,20));
        System.out.println("********************");
        System.out.println(com2.compare(10,20));
    }
    //语法规则六: 当Lambda体只有一条语句时,return 与 括号若有,都可以省略
    @Test
    public void test6(){

        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        };
        Comparator<Integer> com2 = (o1,o2) -> o1.compareTo(o2);

        System.out.println(com1.compare(10,20));
        System.out.println("********************");
        System.out.println(com2.compare(10,20));
    }




}

标签:o2,System,public,Lambda,println,lambda,Java8,表达式,out
来源: https://www.cnblogs.com/lzxlzx11/p/16059096.html