Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Lambda expressions are a new feature of Java8, and although they have been released for a long time, they are definitely not something we will study unless we have to. Now is the time to learn that.

This article focuses on what lambda expressions are, why they exist in Java and how they are used.

In Java object-oriented thinking, we know that functions cannot exist alone. Functions are usually wrapped in an object as a function, and we cannot pass a function as an argument. Java has struggled with this in the past, so let’s look at the following code, which creates a thread and outputs a sentence.

new Thread(new Runnable() {
            @Override
            public void run(a) {
                System.out.println("Hello World !");
            }
        }).start();
Copy the code

You probably know from a basic point of view that this is an anonymous inner class, but as you know, the only code that really works is one line of output. The rest of the code is pretty useless, so if you think about it the other way around, we’re creating a thread to perform a task, a method, so why don’t we just pass in a method?

With that in mind, we can write pseudocode like this.

new Thread((某某任务)).start();
Copy the code

Ok, so now the question is, how do you describe this task, or how do you represent it in Java? Lambda expressions were born.

A function, which may have input arguments, must have a function body, thus defining the lambda expression “(argument 1, argument 2) -> {function body}”.

In practice, however, a lambda expression can have zero or more arguments separated by commas because of differences in arguments and function bodies. Empty parentheses indicate that the parameter is null.

The body of a lambda expression may contain zero or more statements. If the body of a lambda expression has only one statement, curly braces {} may be omitted; otherwise, curly braces {} must be included.

OK, at this point we can override the thread above.

new Thread(
            () -> System.out.println("Hello World ")
          ).start();
Copy the code

Speaking of which, let’s briefly recall what lambda expressions are. There was a time when Java could not take functions as arguments directly, and lambda expressions were created to make them work. Lambda expressions can be thought of as function blocks, but anonymously.

In fact, lambda was created to echo functional programming, which uses functions as subjects to program and treats functions as basic components of code, just like variables. It’s officially called a first class citizen.

Let me give you an example of functional programming.

Calculate the following expression:1 + 2) * 3 - 4Traditional procedural programming might be written like this (silly, for illustration purposes) :int a = 1 + 2;
int b = a * 3;
int c = b - 4; Functional programming requires the use of functions. We can define operations as different functions and write them as follows:int result = subtract(multiply(add(1.2), 3), 4);
Copy the code

Lambda expressions are most commonly used to replace the Runnable interface for thread tasks.

I’m not going to talk about too hard, but I’m going to talk about a simpler iteration for lists.

Operate on each element of a list as follows without using a Lambda expression:

List<Integer> numbers = Arrays.asList(1.2.3.4.5);
for (int element : numbers) {
    System.out.prinln(element);
}
Copy the code

Using Lambda expressions:

List<Integer> numbers = Arrays.asList(1.2.3.4.5);
numbers.forEach(x -> System.out.println(x));
Copy the code

The key is the operation, the input is x, and then the output operation on x. In the case of thread and list iterations, why can they accept lambda expressions as arguments? Let’s look at what’s inside the parameters of the forEach method.

@since 1.8* /@FunctionalInterface
public interface Consumer<T{

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);
Copy the code

Lambda is an interface, and this interface is defined as a functional interface. Lambda expressions can replace functional interfaces. Let’s define a functional interface to demonstrate this.

Define a functional interface:

// Define a functional interface or functional interface
@FunctionalInterface
public interface WorkerInterface {
    // There can be only one abstract method in this interface
    public void doSomeWork(a);
}

public class WorkerInterfaceTest {

    public static void execute(WorkerInterface worker) {
        worker.doSomeWork();
    }

    public static void main(String[] args) {

        // invoke doSomeWork using Annonymous class
        execute(new WorkerInterface() {
            @Override
            public void doSomeWork(a) {
                System.out.println("Worker invoked using Anonymous class"); }});// invoke doSomeWork using Lambda expression
        execute(()->{System.out.println("Worker invoked using Lambda expression");});
    }
}
Copy the code

To summarize the first few questions raised, lambda expressions can be thought of as anonymous functions, and we can substitute lambda expressions for functional interfaces (such as the Runnable interface). Functional programming is a programming pattern that Java defines lambda to support. Lambda is mainly used for alternative functional interfaces, list iteration, and some operations on collections.