Lambda expression basics recently learned

Lambda expression basics recently learned

Preface

Only a bald head can become stronger.
The text has been included in my GitHub repository, welcome to Star: https://github.com/ZhongFuCheng3y/3y I
went to Shanghai during the Mid-Autumn Festival and punched a card on the Bund:

Lambda expression basics recently learned
The Bund
then learned about functional programming in Java, sorted it out for everyone, and studied together!

One, Lambda usage

When I wrote the Optional class before, I briefly talked about how Lambda is used. Here is a review with you. The syntax of Lambda is like this:

Lambda expression basics recently learned
The syntax
to create a thread to distinguish Lambda syntax to create threads and anonymous inner classes (the code is clearly a lot less!):

public static void main(String[] args) {
    // 用匿名内部类的方式来创建线程
    new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("公众号:Java3y---关注我!");
        }
    });

    // 使用Lambda来创建线程
    new Thread(() -> System.out.println("公众号:Java3y---关注我!"));
}

Using Lambda expressions actually creates an instance object of the interface.

Lambda expression basics recently learned
Return a Runnable object instance
icon; Runnable interface as an example:

Lambda expression basics recently learned
Taking the Runnable interface as an example, the
use of Labmda expressions requires a functional programming interface. For example, on the Runnable interface we can see the @FunctionalInterface annotation (marking that this interface has only one abstract method)

Lambda expression basics recently learned
Functional programming interface has only one abstract method

1.1 Functional programming interface

As can be seen from the above code example, when we use Lambda expressions to create threads, we don't care about interface names, method names, and parameter names. We only pay attention to his parameter types, the number of parameters, and the return value.

JDK natively provides us with some functional programming interfaces for us to use, the following are some commonly used interfaces:

Lambda expression basics recently learned
A
brief description of commonly used functional programming interfaces :

  • The unary interface in the table means that there is only one
    Lambda expression basics recently learned
    input parameter, and the binary interface means that there are two input parameters. Take BiFunction as an example of the
    Lambda expression basics recently learned
    commonly used functional interface
    Demo:
// Consumer 一个入参,无返回值
Consumer<String> consumer = s-> System.out.println(s);
consumer.accept("Java3y");

// Supplier 无入参,有返回值
Supplier<String> supplier = () -> "Java4y";
String s = supplier.get();
System.out.println(s);

//.....

When using Lambda, there are two things to remember:

  1. Lambda returns an instance object of the interface
  2. Are there any parameters, how many parameters are there, whether there is a return value, what is the type of the return value -> choose your own suitable functional interface

    1.2 Method reference

When learning Lambda, you may also find a strange way of writing, such as the following code:

// 方法引用写法
Consumer<String> consumer = System.out::println;
consumer.accept("Java3y");

If you follow the normal Lambda writing, it may be like this:

// 普通的Lambda写法
Consumer<String> consumer = s -> System.out.println(s);
consumer.accept("Java3y");

Obviously, using method references is a bit more concise than ordinary Lambda expressions.

If the implementation of the functional interface happens to be achieved by calling a method, then we can use the method reference

Lambda expression basics recently learned
The realization of the functional interface can happen by calling a method to realize the
method reference and there are several types:

  • Method references for static methods
  • Method references for non-static methods
  • The method reference of the constructor
    method reference Demo:
public class Demo {
    public static void main(String[] args) {
        // 静态方法引用--通过类名调用
        Consumer<String> consumerStatic = Java3y::MyNameStatic;
        consumerStatic.accept("3y---static");

        //实例方法引用--通过实例调用
        Java3y java3y = new Java3y();
        Consumer<String> consumer = java3y::myName;
        consumer.accept("3y---instance");

        // 构造方法方法引用--无参数
        Supplier<Java3y> supplier = Java3y::new;
        System.out.println(supplier.get());
    }
}

class Java3y {
    // 静态方法
    public static void MyNameStatic(String name) {
        System.out.println(name);
    }

    // 实例方法
    public void myName(String name) {
        System.out.println(name);
    }

    // 无参构造方法
    public Java3y() {
    }
}

The results are as follows:

Lambda expression basics recently learned
result

At last

Although the code of Lambda looks concise, it is difficult to understand if it is complicated.

When learning Lambda, we must first know what are the commonly used functional programming interfaces, and what are the differences between these functional programming interfaces (number of parameters, return value types)

Lambda expressions return an instance of an interface object. If the implementation of a functional interface happens to be achieved by calling a method, then we can use method references instead of Lambda expressions

Finally, give a complete example:

// Supplier是一个无入参带返回的值的函数式编程接口

// () -> new Java3y()这整句Lambda表达式,返回的是Supplier接口的实例。从Lambda表达式可以看出无参数,带返回值
Supplier<Java3y> supplier = () -> new Java3y(); 

// 由于这个“() -> new Java3y()”Lambda表达式可以通过调用一个方法就实现了,那么我们可以优化成方法引用
Supplier<Java3y> supplier2 = Java3y::new;

Public number article navigation: two years of hard work!

Lambda expression basics recently learned

More than 200 original articles technical articles
massive video resources
exquisite mind map
face questions
press scan code may be concerned about obtaining
watching and sharing is very important to me!

Guess you like

Origin blog.51cto.com/15082392/2590335