JDK8 (two) lambda expression

1, Lambda Profile

Lambda expressions are Java 8 most popular and commonly used features.
It will introduce the concept of functional programming Java, the benefits of functional programming is that it can help us save a lot of code, very easy to use, can greatly improve our coding efficiency.

2. Notice

2.1 Interface -> default method

Keyword default was modified method requires the method body.
Such an approach would all subclasses default implementation (do not have to write), if you want to overwrite can be overwritten in the implementation class.
the role of default keyword is used to extend.

/**
 * 从java8开始,接口当中允许定义default默认方法
 * 修饰符:public default(public可以省略,default不能省略)
 */
public interface MyInterface {
	void method1();
    void method2();
    // 接口中的一个默认方法
    default void methodNew() {
        System.out.println("接口默认方法执行");
    }
}

2.2 Interface -> static method

default (default method), all subclasses will default implementation.
However, static methods of the interface implementation class will not be inherited (to achieve).

/**
 * 从java8开始,接口当中允许定义静态方法
 * 修饰符:static xxx
  * 一般类的静态方法用法相同
 */
public interface Animal {
    void eat();
	// 接口中的一个静态方法
    static Animal getAnimal() {
        return new Cat();
    }
}

2.3 Functions Interface

Function interface, in Java means: one and only one method of abstract interfaces.
Function interface, an interface that is suitable for functional programming scenarios.
The functional programming in Java is reflected Lambda, so the function interface is that you can apply to interfaces Lambda use.
The benefits of functional programming is that it can help us save a lot of code, very easy to use, can greatly improve our coding efficiency.
Only by ensuring the interface and only one abstract method, Java can be deduced in the Lambda smoothly.

2.3.1 function annotation interface format @FunctionalInterface

Format:
i.e. the interface, and only a method of abstract interface to.
For example, the Thread run () method.

Public interface 接口名称 {
    返回值 方法名称();
}

@FunctionalInterface notes
and some annotations are in compilation of work, such as @Override comment.
@FunctionalInterface notes, also works at compile time.
The annotation is specifically for new annotations java8 interface function is introduced, acting on an interface.
By using this annotation to define interfaces, compile-time checking whether the conditions will force the interface complies with the functional interface, does not meet it will error.
Note:
Even if the annotation is not used, as long as the definition function interface, which is a function interface.

3, lambda expressions

3.1, lambda expressions format

Format:
similar function ES6 arrow.

(param1,param2,param3 ...,paramN)-  > { 
 	 //代码块;  
  }

Note:

  1. lambda expression is a function expressed interface;
  2. Arrow to the left, is a function of the comma-separated form of parameters list;
  3. Arrow on the right is a function body;

Principle omitted 3.2, lambda expressions

1, parameter type may be omitted.
However, while omitting only the type of all parameters, or simply not be omitted.
2, if the parameter of the parameter list, and only one, then the parentheses may be omitted.
3, if the statements within the curly braces, and only one, then, whether or not there is a return value , return , braces , semicolons can be omitted

3.3, the original wording

package com.zicheng.lambda;

/**
 * 子诚
 * Description:原始写法
 * 时间:2020/3/30 9:38
 */
public class LambdaDemo {
    //抽象功能接口
    interface Printer {
        void print(String val);
    }

    //通过参数传递功能接口
    public void printSomething(String something, Printer printer) {
        printer.print(something);
    }

    //通过创建对象调用函数
    public static void main(String[] args) {
        LambdaDemo demo = new LambdaDemo();
        String something = "I am learning Lambda";
        //实现Printer接口,内部类的方式
        Printer printer = new Printer() {
            @Override
            public void print(String val) {
                //控制台打印
                System.out.println(val);
            }
        };
        //调用接口打印
        demo.printSomething(something, printer);
    }
}

Here Insert Picture Description

3.4, lambda expressions wording

package com.zicheng.lambda;

/**
 * 子诚
 * Description:
 * 时间:2020/3/30 10:27
 */
public class LambdaDemo2 {
    //抽象功能接口
    interface Printer {
        void print(String val);
    }

    //通过参数传递功能接口
    public void printSomething(String something, Printer printer) {
        printer.print(something);
    }

    //通过创建对象调用函数
    public static void main(String[] args) {
        LambdaDemo2 demo2 = new LambdaDemo2();
        String something = "I am learning Lambda";
        //实现Printer接口,内部类的方式
        Printer printer = (String val) -> {
            //控制台打印
            System.out.println(val);
        };
        //调用接口打印
        demo2.printSomething(something, printer);
    }
}

Here Insert Picture Description

3.5, 3.4 for lambda expressions, and optimizing

3.4 code

 		//实现Printer接口,内部类的方式
        Printer printer = (String val) -> {
            //控制台打印
            System.out.println(val);
        };

3.5.1 Type parameter is omitted, but since only one parameter, the parentheses be omitted

		//实现Printer接口,内部类的方式
        Printer printer = val -> {
            //控制台打印
            System.out.println(val);
        };

3.5.2 Since the method body, only one statement, the braces are omitted

		//实现Printer接口,内部类的方式
        Printer printer = val -> System.out.println(val);
Published 44 original articles · won praise 5 · Views 887

Guess you like

Origin blog.csdn.net/qq_40634246/article/details/105192256