Basic use of Lambda (understand the definition from the code)

1. Lambda expression

​ Lambda expression is an anonymous function (this is not completely correct for Java, but I think so for now), is a new feature of JDK1.8, simply put, it is a method without declaration, that is, no access Modifiers, return value declarations and names.

​ You can think of it as a shorthand, and write it where you need to use a certain method. When a method is used only once, and the definition is short, using this shorthand alternative is especially effective, so that you don't have to laboriously write declarations and methods in the class.

1.1 Basic party implementation class method

First look at the basic implementation class, generally define the interface and the three methods of implementing the interface (general implementation class, static internal class, local internal class).

package SunLambda;
//对接口实现类进行调用。
public class Lambda_01 {
    
    
    //静态内部类
     static class ILike2 implements LambdaSun{
    
    
        @Override
        public void Lambda() {
    
    
            System.out.println("学习Lambda让我快乐!2");
        }
    }
    public static void main(String[] args) {
    
    
        ILike iLike = new ILike();
        iLike.Lambda();
        ILike2 iLike2 = new ILike2();
        iLike2.Lambda();

        //局部内部类
        class ILike3 implements LambdaSun{
    
    
            @Override
            public void Lambda() {
    
    
                System.out.println("学习Lambda让我快乐!3");
            }
        }
        ILike3 iLike3 = new ILike3();
        iLike3.Lambda();
    }
}

//定义接口
interface LambdaSun{
    
    
    void Lambda();
}

//定义接口实现类
class ILike implements LambdaSun{
    
    
    @Override
    public void Lambda() {
    
    
        System.out.println("学习Lambda让我快乐!");
    }
}

Insert picture description here

Why use lambda?

Use lambda expressions to concisely implement an interface.

Lambda requirements for interfaces

There can be only one abstract method defined in the interface that must be implemented (must be implemented, not modified by default).

1.2 lambda simplified no-parameter construction method

package SunLambda;

public class Lambda_02 {
    
    


    public static void main(String[] args) {
    
    
        //匿名内部类,没有类的名称,必须借助接口或者是父亲
        LambdaSun1 lambda = new LambdaSun1() {
    
    
            @Override
            public void Lambda() {
    
    
                System.out.println("学习Lambda让我快乐!5");
            }
        };
        lambda.Lambda();
        //用Lambda方法进行简化

        LambdaSun1 lambd = () ->{
    
    
                System.out.println("学习Lambda让我快乐!6");
        };
        lambd.Lambda();
        LambdaSun1 lambd1;

        //用Lambda方法进行简化简化2
        lambd1= ()-> System.out.println("学习Lambda让我快乐!7");
        lambd1.Lambda();

    }

}
//定义接口
interface LambdaSun1{
    
    
    void Lambda();
}


Insert picture description here

Lambda implements the interface and is implemented step by step with anonymous inner classes.

Note:

1. Lambda expression can be simplified into one line only if there is only one line of code. If there are multiple lines, code blocks (that is {}) must be used;

2. The premise is that the interface is a functional interface;

3. Multiple parameter types of the interface can be removed. If you want to remove all parameter types, you must add parentheses.

1.3 Lambda simplified method with parameters

package SunLambda;

public class Lambda_03 {
    
    



    public static void main(String[] args) {
    
    
    //匿名内部类
        LambdaSun2 lambdasun = new LambdaSun2(){
    
    
            @Override
            public void lambda(int a,int b,int c) {
    
    
                System.out.println(a+b+c);
            }
        };
        lambdasun.lambda(1,1,1);
        LambdaSun2 lambda;
        //lambda简化1
        lambda =(int a,int b,int c)->{
    
    
            System.out.println(a+b+c);
        };
        lambdasun.lambda(2,2,2);
        //lambda简化2
        lambda =( a,b,c)->{
    
    
            System.out.println(a+b+c);
        };
        lambdasun.lambda(3,3,3);
        //lambda简化3
        lambda=( a,b,c)-> System.out.println(a+b+c);
        lambdasun.lambda(4,4,4);
    }
}
interface LambdaSun2{
    
    
    void lambda(int a,int b,int c);
}

Insert picture description here

Note: Multiple parameter types of the interface can be removed. If you want to remove them, you can remove them, but you must add parentheses.

Self-learning in Bilibili mad God said JAVA (invasion and deletion);

As your own notes, welcome everyone!

Guess you like

Origin blog.csdn.net/SUN__CGJ/article/details/109406652
Recommended