1. Functional programming idea

Object-oriented thinking:

  • Do something, find an object that solves the problem, and call its methods to do it

Functional programming ideas:

  • It doesn’t matter who does what and how you do it as long as you get the result you want. It’s the result, not the process. Right

2. Introduction of Lambda expressions

Why Lambda expressions?

  • Avoid overdefining anonymous inner classes

  • Make code that implements the same logic look cleaner

Functional interface: An interface that contains only one abstract method is a functional interface

Such as:

public interface Runnable {
    public abstract void run(a);
}
Copy the code

Lambda is a new feature introduced in JDK8. Functional interfaces are the key to Lambda expressions

With a functional interface, we can create objects for that interface through lambda expressions.

Typically, for an interface and implementation class, we would write:

interface Lambda {
    void lambda(a);
}

class MyLambda implements Lambda {

    @Override
    public void lambda(a) {
        System.out.println("Learning lambda expressions..."); }}public class LambdaLearn {
    public static void main(String[] args) {
        Lambda lb = newMyLambda(); lb.lambda(); }}Copy the code

Learning lambda expressions…

We need to write three classes to implement the interface, which would be tedious to look at in terms of functional programming. Thus, we can improve with inner classes:

I’m glad the interviewer hasn’t asked me yet

Improvement 1: Static inner classes

Put the code from the MyLambda class into the LambdaLeadn to become a static inner class

interface Lambda {
    void lambda(a);
}

public class LambdaLearn {

    // Static inner class
    static class MyLambda implements Lambda {
        @Override
        public void lambda(a) {
            System.out.println("Learning lambda expressions..."); }}public static void main(String[] args) {
        Lambda lb = newMyLambda(); lb.lambda(); }}Copy the code

Learning lambda expressions…

And then we can simplify

Improvement 2: Member inner classes

Turn MyLambda into a member inner class

interface Lambda {
    void lambda(a);
}

public class LambdaLearn {
    public static void main(String[] args) {
        // Member inner class
        class MyLambda implements Lambda {
            @Override
            public void lambda(a) {
                System.out.println("Learning lambda expressions...");
            }
        }

        Lambda lb = newMyLambda(); lb.lambda(); }}Copy the code

Let’s try it again. Can we simplify it?

Improvement 3: Anonymous inner classes

Interfaces are implemented directly through anonymous inner classes

interface Lambda {
    void lambda(a);
}

public class LambdaLearn {
    public static void main(String[] args) {
        // Member inner class
        new Lambda() {
            @Override
            public void lambda(a) {
                System.out.println("Learning lambda expressions..."); } }.lambda(); }}Copy the code

We simplified the code by making three improvements to the inner class to achieve the same functionality. However, if the anonymous inner class defines a lot, the functionality is implemented, but the code is unreadable. So, we introduced lambda expressions on this basis

Lambda expression pushdown

interface Lambda {
    void lambda(a);
}

public class LambdaLearn {
    public static void main(String[] args) {
        Lambda lb = () -> {
            System.out.println("Learning lambda expressions..."); }; lb.lambda(); }}Copy the code

Semantic analysis

  • The preceding pair of parentheses is the argument to the lambda method (none), indicating that no conditions are required;

  • An arrow in the middle -> represents passing the previous argument to the following code;

  • The code in {} is the method body that overrides the abstract method in the interface

So, can lambda be simplified any more?

3. Simplification of lambda expressions

Reduced parameter type

Define a lambda expression with parameters to simplify

interface Lambda {
    void lambda(String str);
}

public class Simplify {
    public static void main(String[] args) {
        Lambda lb = (String str) -> {
            System.out.println(str);
        };
        lb.lambda("Lambda expression"); }}Copy the code
  • Reduced parameter type

  • Simplify the parentheses

  • Simplified code block {}

It cannot be simplified if there are multiple parameters

Lambda expressions implement the Runnable interface

public class LThread {
    public static void main(String[] args) {
        // Implement multithreading with anonymous inner classes
        new Thread(new Runnable() {
            @Override
            public void run(a) {
                System.out.println("Thread" + Thread.currentThread().getName() + "Created");
            }
        }).start();

        // Lambda implements multithreading
        new Thread(()-> {
                System.out.println("Thread" + Thread.currentThread().getName() + "Created"); } ).start(); }}Copy the code

The Thread Thread-0 was created

Thread 1 was created