lambda expression written

lambda expression written

lambda must have an interface, and the interface is only an abstract method

lambda consists of three parts:
1. Parameter
2 .->
3. snippet
format is as follows:
(Parameter Type name) -> {} snippet
Where: parameter type parameter name is not required, if there are multiple, separated by commas. -> is a lambda syntax specific requirements

Shorthand rule of lambda

1: Parameter Type in parentheses may be omitted.
2: If the parentheses and only one parameter, the parentheses may be omitted.
3: If the braces and only one statement, regardless of whether there is a return value, you can omit the braces, return keywords and semicolons.

Difference between traditional writing of code and lambda expressions
package cn.hp.lambda;

public class Lamdba {
    public static void main(String[] args) {

        new Thread(new Runnable() {
            /**
             * 采用 接口方式启动线程对象
             * 1:必须new 接口对象Runnable
             * 2:必须重写run方法
             * 3:必须调用start() 启动线程
             */
            @Override
            public void run() {
                System.out.println("传统启动线程方法");
            }
        }).start();

        /**
        *lamdba表达式写法
        *不必new接口对象
        *不必重新run方法
        */
       new Thread(()->{System.out.println("传统启动线程方法");}).start();
        //()代表run函数 进一步简写省略(大括号:{})
       new Thread(()->System.out.println("传统启动线程方法")).start();
    }
}

Parentheses only one parameter, parentheses may be omitted

package cn.hp.lambda;
/**
 * 测试
 * 小括号仅有一个参数,小括号可以省略
 */
public interface Lamdba4 {
    void test(String str);
}

class Lamdba4Impl {
    public static void main(String[] args) {
        Lamdba4 l4 =(ss)->{ System.out.println(ss);};
        //简写省略 大括号
        Lamdba4 l5 =ss->System.out.println(ss);
		//调用方法
        l4.test("hello world");
    }
}

No reference no return Lamdba value of the expression

package cn.hp.lambda;

/**
 *  无参无返回值 指的是,接口中的方法
 */
public interface Lamdba2 {
    void pint();//提供一个无参无返回值的 方法
}
class Lamdba2Impl{
    public static void main(String[] args) {
        /**
         * 提供了一个接口对象 l2
         *  等号右侧 第一个()代表pint()
         */
        Lamdba2 l2 = ()->{System.out.println("打印字字符串");};
 //进一步简写 省略大括号
        Lamdba2 l2=()-> System.out.println("打印字字符串");

        l2.pint();//调用pint方法
    }
}

There have Lamdba expression returns parameter values

lambda requirements, the method includes a parameter or variable type must lambda corresponding interface type to use as the lambda interface instance

package cn.hp.lambda;

/**
 * 有参有返回值
 */
public interface Lamdba3 {
    int add (int x,int y);
}

class Lamdba3Impl{
    public static void main(String[] args) {
        textAdd((int a,int b)->{return a+b;});
       //进一步简写 省略参数类型 return 和大括号
        textAdd((a,b)->a+b);
    }
    //建立与接口的联系
    public static  void textAdd(Lamdba3 l3){
        int result=l3.add(10,13);
        System.out.println("求和"+result);
    }
}
Published 68 original articles · won praise 7 · views 2518

Guess you like

Origin blog.csdn.net/Cui6023056/article/details/104819121