Evolution of Lambda Expression in Java

  • 2021-12-09 08:57:07
  • OfStack

Evolution of Directory Lambda Expressions Why Use Lambda Expressions Lambda Expressions Note the following is the implementation process of Lambda Expressions 1. The first use is to define external implementation classes to complete interfaces 2. Start using static internal classes to implement 3. Use local internal classes to use 4. Use anonymous internal classes to implement interfaces 5. Finally use Lambda expressions to implement functional expressions Interface summary

Evolution of Lambda Expression

Why use Lambda expressions

The code can be concise and the readability of the code can be improved It can avoid logic disorder caused by too many anonymous internal class definitions When the interface abstraction method was originally implemented, It needs to be realized by defining an external class that implements the interface, then it becomes an internal static class, then it becomes a local internal class, and then it becomes an anonymous internal class. Finally, in order to make the code more concise, Lambda expression is deduced, and finally it realizes the effect of using one line of code to complete the previous multi-line code

Notices of Lambda Expression

Lambda expression actually belongs to the concept of functional programming, so you should know whether it belongs to functional programming when using it The implementation of an Lambda expression depends on the interface and the parent class, so you must have one of the two to implement an Lam expression Only one abstract method is required in the interface implemented by Lambda expression. If there are more than one abstract method, Lambda expression cannot be used to program Lambda expressions are suitable for both non-parametric and parametric methods Lambda expressions first appeared in JDK 8, so they are only supported after JDK 8

The following is the implementation of Lambda expression

1. The first thing you use is to define an external implementation class to complete the interface

public class tt1 {
    public static void main(String[] args) {
        // To implement an interface with an external class, you first need to define another 1 Classes, and then create objects in inner classes 
        // This is for those who only need to use 1 The interface is more troublesome, and it will also make the whole code bloated and make it difficult for other developers to read 
        lover l1 = new lover();
        l1.love();
    }
}
// Defining an interface 
interface ILove {
    void love();
}
// External implementation class 
class lover implements ILove{
    @Override
    public void love() {
        System.out.println("I love you my lover ---> 1");
    }
}

//Output: I love you lover-- > 1

2. Start using static inner classes to implement

public class tt1 {
    // Static inner class 
    static class lover2 implements ILove{
        @Override
        public void love() {
            System.out.println("I love you my lover ---> 2");
        }
    }
    public static void main(String[] args) {
        // To implement an interface with an external class, you first need to define another 1 Classes, and then create objects in inner classes 
        // This is for those who only need to use 1 The interface is more troublesome, and it will also make the whole code bloated and make it difficult for other developers to read 
        ILove l1 = new lover1();
        l1.love();
        // Using static inner classes, because the implementation class and main Methods are located in the same main class, which is convenient for developers to read, but the implementation process is still cumbersome 
        ILove l2 = new lover2();
        l2.love();
    }
}
// Definition 1 Functional interface 
interface ILove {
    void love();
}
// External implementation class 
class lover1 implements ILove{
    @Override
    public void love() {
        System.out.println("I love you my lover ---> 1");
    }
}

//Output: I love you my lover-- > 1
// I love you my lover --- > 2

3. Use local inner classes to use

public class tt1 {
    // Static inner class 
    static class lover2 implements ILove{
        @Override
        public void love() {
            System.out.println("I love you my lover ---> 2");
        }
    }
    public static void main(String[] args) {
        // To implement an interface with an external class, you first need to define another 1 Classes, and then create objects in inner classes 
        // This is for those who only need to use 1 The interface is more troublesome, and it will also make the whole code bloated and make it difficult for other developers to read 
        ILove l1 = new lover1();
        l1.love();
        // Using static inner classes, because the implementation class and main Methods are located in the same main class, which is convenient for developers to read, but the implementation process is still cumbersome 
        ILove l2 = new lover2();
        l2.love();
        // Local inner class 
        class lover3 implements ILove{
            @Override
            public void love() {
                System.out.println("I love you my lover ---> 3");
            }
        }
        ILove l3 = new lover3();
        l3.love();
    }
}
// Definition 1 Functional interface 
interface ILove {
    void love();
}
// External implementation class 
class lover1 implements ILove{
    @Override
    public void love() {
        System.out.println("I love you my lover ---> 1");
    }
}

//Output: I love you my lover-- > 1
// I love you my lover --- > 2
// I love you my lover --- > 3

4. Implement interfaces using anonymous inner classes

public class tt1 {
    // Static inner class 
    static class lover2 implements ILove{
        @Override
        public void love() {
            System.out.println("I love you my lover ---> 2");
        }
    }
    public static void main(String[] args) {
        // To implement an interface with an external class, you first need to define another 1 Classes, and then create objects in inner classes 
        // This is for those who only need to use 1 The interface is more troublesome, and it will also make the whole code bloated and make it difficult for other developers to read 
        ILove l1 = new lover1();
        l1.love();
        // Using static inner classes, because the implementation class and main Methods are located in the same main class, which is convenient for developers to read, but the implementation process is still cumbersome 
        ILove l2 = new lover2();
        l2.love();
        // Local inner class 
        class lover3 implements ILove{
            @Override
            public void love() {
                System.out.println("I love you my lover ---> 3");
            }
        }
        ILove l3 = new lover3();
        l3.love();
        // Using internal anonymous classes 
        ILove l4 = new ILove() {
            @Override
            public void love() {
                System.out.println("I love you my lover ---> 4");
            }
        };
    }
}
// Definition 1 Functional interface 
interface ILove {
    void love();
}
// External implementation class 
class lover1 implements ILove{
    @Override
    public void love() {
        System.out.println("I love you my lover ---> 1");
    }
}

//Output: I love you my lover-- > 1
// I love you my lover --- > 2
// I love you my lover --- > 3
// I love you my lover --- > 4

5. Finally, use Lambda expression to implement functional interface

public class tt1 {
    // Static inner class 
    static class lover2 implements ILove{
        @Override
        public void love() {
            System.out.println("I love you my lover ---> 2");
        }
    }
    public static void main(String[] args) {
        // To implement an interface with an external class, you first need to define another 1 Classes, and then create objects in inner classes 
        // This is for those who only need to use 1 The interface is more troublesome, and it will also make the whole code bloated and make it difficult for other developers to read 
        ILove l1 = new lover1();
        l1.love();
        // Using static inner classes, because the implementation class and main Methods are located in the same main class, which is convenient for developers to read, but the implementation process is still cumbersome 
        ILove l2 = new lover2();
        l2.love();
        // Local inner class 
        class lover3 implements ILove{
            @Override
            public void love() {
                System.out.println("I love you my lover ---> 3");
            }
        }
        ILove l3 = new lover3();
        l3.love();
        // Using internal anonymous classes 
        ILove l4 = new ILove() {
            @Override
            public void love() {
                System.out.println("I love you my lover ---> 4");
            }
        };
        l4.love();
        // Use Lambda Expression implementation interface 
        ILove l5 = () ->{
            System.out.println("I love you my lover ---> 5");
        };
        l5.love();
    }
}
// Definition 1 Functional interface 
interface ILove {
    void love();
}
// External implementation class 
class lover1 implements ILove{
    @Override
    public void love() {
        System.out.println("I love you my lover ---> 1");
    }
}

//The output is:
I love you my lover --- > 1
I love you my lover --- > 2
I love you my lover --- > 3
I love you my lover --- > 4
I love you my lover --- > 5

More extreme simplification of Lambda expressions (for Lambda expressions with parameters)

Simplified Data Types in an Lambda expression, you can omit the data type of a parameter, leaving only one data name. More special is that if there are multiple parameters, the data types of all parameters should be omitted when omitting, otherwise, all parameters will not be omitted, and the parameters need to be included in parentheses. Refer to the above item for omitting parentheses. You can omit parentheses and curly braces only when one parameter requires them In Lambda expressions, curly braces can only be omitted if the output statement or code is only 1 line. If there are multiple pieces of execution code, you still need to include the code in curly braces

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: