Java8 (a) - lambda expression

  I believe as a Java programmer will be more or less understood the Java8 the lambda expression, functional programming, I also used the lambda expressions, it is relatively simple to use

Implementation

An example of going through all feel lambda:

Comparator<Student> comparator = new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getStudentAge() - o2.getStudentAge();
    }
};
Comparator<Student> comparator1 = (Student o1, Student o2) -> o1.getStudentAge() - o2.getStudentAge();

First, what is a lambda expression?

  Can be understood as a way to express succinctly anonymous function can be passed: it has no name, but it has a list of parameters, the main function, the return type, there is a list of exceptions that may be thrown.

1. Anonymous:

  We say anonymous, because it is not normal methods have a clear name: write less and want much more!

2, function:

  We say that it is a function, not because of Lambda function method as belonging to a particular class. But the same and methods, Lambda has a list of parameters, the main function, the return type, may also be

The list of exceptions thrown.

3, pass:

  Lambda expressions passed as arguments to the method or stored in a variable.

4, simple:

  Without having to write a lot like the anonymous class as the template code.

The above definition of a bit long, we are mainly to understand the characteristics of a lambda. lambda word from academia developed to describe a computing algorithm λ

Two, lambda expression structure

1, parameters: The parameters of the Comparator compare methods, two Student.

2, arrows: Arrow -> the parameter list and spaced Lambda body.

3, Body: compare the age of two Student. Lambda expression is the return value.

The basic syntax

. 1, (Parameters) -> expression The

 2, (Parameters) -> {statements;}   // Note braces statement

For chestnuts

(String s) -> s.length()
(int x, int y) -> {
 System.out.println("Result:");
 System.out.println(x+y);
}

The first example: String Type parameter and returns an int. Lambda is no return statement, as it has been implicit return, corresponding to the first syntax

The second example: having two int type reference number and no return value (void returns). Note Lambda expressions can contain multiple lines of statements, corresponding to the second syntax

Examples of errors:

() -> { return "Mario";}                   // This is also true lambda, explicit return String 
 (Integer I) -> return "Alan" + I;       // incorrect lambda, requires the use of braces 
 (String s) -> { "IronMan";}              // this is an expression, not a statement can be changed 
 (String s) -> "Iron Man" or (String S) -> { return "IronMan";}

Third, where the use, how to use lambda

Functional Interface

  There is only one interface and abstract methods, such as Runnable, Callable

  Lambda expressions allow to provide a form of inline function is implemented as an abstract method of the interface, and the whole expression as a function of the interface instances

For chestnuts

R1 = the Runnable () -> System.out.println ( "the Hello World. 1");  // Use lambda expression 
the Runnable R2 = new new the Runnable () {                  // use anonymous inner classes 
    public  void RUN () { 
        the System.out. the println ( "the Hello World 2" ); 
    } 
};

PS: default, static method is not, and still is functional interfaces

@FunctionalInterface
public interface abc{

    void add();
    static void del() {
        System.out.println("aaa");
    };
    default void update(){
        System.out.println("bbb");
    };
}

Functions descriptor

  Abstract method signature function interface is basically signature Lambda expressions. We call this function is called abstract method descriptor.

E.g:

  1, Runnable interface has only a RUN (), and the return value is void, so that the signature is () -> void , the representative parameter is empty, returning void

  2, Callable interface has only one call (), and the return value is T (you can go by instead of String or other data types), the signature is () -> String

To a negative example

execute(() -> "abc");                        //返回值定义为String,签名() -> String,就和Runnable中的抽象方法run的签名不相匹配了
public void execute(Runnable r){
    r.run();
}                

  Lambda表达式可以被赋给一个变量,或传递给一个接受函数式接口作为参数的方法,当然这个Lambda表达式的签名要和函数式接口的抽象方法一样

举个栗子

public void process(Runnable r){
    r.run();
}
process(() -> System.out.println("This is awesome!!"));

结果:

This is awesome!!

相当于

test.process(new Runnable() {
    @Override
    public void run() {
        System.out.println("This is awesome!!");
    }
});

@FunctionalInterface

  表示该接口会设计成一个函数式接口,如果不是函数式接口,注解就会报错,表示存在多个抽象方法,这个注解不是必须的,就像

@Override一样

很多接口在Java8中都添加了这个注解

@FunctionalInterface
public interface Comparator<T> {}

@FunctionalInterface
public interface Runnable {)

 

Guess you like

Origin www.cnblogs.com/huigelaile/p/10992915.html