Recently learned Lambda expressions

Foreword

Only the head can become strong.

Text has been included to my GitHub repository, welcome Star: github.com/ZhongFuChen...

Mid-Autumn Festival went to lie down Shanghai, called the outer beach card:

Bund

Then learn about the function of Java programming, for everyone to order a bit, learn together!

A, Lambda usage

Optional before writing this class, simply said, is about how to use Lambda, where we talk together about the review, Lambda syntax is as follows:

grammar

Lambda syntax to create threads and anonymous inner classes to create differences threads ( apparently the code 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---关注我!"));
}
复制代码

Use Lambda expressions, the actual instance of the object is to create the interface .

Returns a Runnable object instance

Shown; for example the Runnable interface:

Runnable interface as an example to

Use Labmda expression requires functional programming interfaces , such as Runnable interface, we can see the @FunctionalInterfacenotes (marked this interface is only an abstract method)

Functional programming interface is only an abstract method

1.1 Functional Programming Interface

As can be seen from the code above example, we use Lambda expressions when creating a thread, I do not care about the interface names, method names, parameter name . We are only concerned about his parameter type, number of parameters, return values .

JDK native gave us some functional programming interfaces to help us to use, here are some common interface:

Common functional programming interfaces

Briefly explain:

  • Unary table represents only one interface to the reference binary interface represents the two parameters

For example to BiFunction

Common interface function

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, just two points to remember:

  1. Lambda is returned instance object interface
  2. There are no parameters, the number of parameters, return values or need, what type of return value is ----> choose the appropriate function interface

1.2 reference method

When I learn Lambda also you may find a more strange wording, for example, the following code:

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

If the normal Lambda wording might look like this:

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

Obviously use references than the average of a number of Lambda expressions and concise.

If you implement functional interface can be achieved just by calling a method , then we can use the method reference

Exactly implement interface function may be implemented by calling a method

The method is divided into several references:

  • Method references a static method
  • The method of non-static method of reference
  • REFERENCE methods Constructor

Method references 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:

result

At last

Lambda Although the code looks simple, but if complex, it is quite ugly understand.

When learning Lambda, first of all we need to know what are commonly used in functional programming interfaces, what difference these functional programming interfaces (number of parameters, return type)

Lambda expressions returns the interface object instance , if realized functional interface can be achieved just by calling a method , then we can use the method reference instead of Lambda expressions

Finally, a complete example:

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

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

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

复制代码

Willing output of dry cargo of Java technology public number: Java3y . A public number more than 200 original articles technical articles, massive video resources, beautiful mind map, attention can get!

Forwarded to the circle of friends is the biggest support for me!

I think the article is well written, points praise !

Guess you like

Origin juejin.im/post/5d881cd8f265da03f04d0a6b