This is the 9th day of my participation in the More text Challenge. For details, see more text Challenge

This article is participating in the “Java Theme Month – Java Development in action”. See the link to the event for more details


Related articles

Java Multithreading: Java multithreading


preface

Lamda expressions support only functional interfaces.

  • Lambda expressions are a new feature in JDK8 that can replace most anonymous inner classes and write more elegant Java code, especially for collection traversal and other collection operations, greatly optimizing code structure.
  • Why break in with a study of lamda expressions, because it’s awesome. The code feels like a big guy.
One, functional interface
  • What is a functional interface?

    • Any interface that has only one abstract method is a functional interface.
  • Typical functional interface

public interface mainInterface {
    void funMethod(a);
}
Copy the code
  • For a functional interface, we can use a LAMDA expression to create its implementation object.
Two, the five implementations of the interface
  • (1) Normal class implementation

Code examples:

/** * 1. Normal interface implementation class */
class FunInterImpl implements FunInter{
    // Override this method
    @Override
    public void method(String name) {
        System.out.println("I am implementing the override method in the class:"+name); }}Copy the code
  • ② Static inner class implementation
//2. Static inner class implementation
    static class FunInterImpl implements FunInter{
        @Override
        public void method(String name) {
            System.out.println("I am implementing the override method in the class:"+name); }}Copy the code
  • ③ Local inner class implementation
//3. Local inner class
        class FunInterImpl2 implements FunInter{
            @Override
            public void method(String name) {
                System.out.println("I am implementing the override method in the class:"+name); }}Copy the code
  • (4) Anonymous inner class implementation
//4. Anonymous inner class, want a semicolon; At the end
        FunInter funInter2 = new FunInter() {
            @Override
            public void method(String name) {
                System.out.println("I am implementing the override method in the class:"+name); }};Copy the code
  • ⑤ LamDA expression implementation
//5. Lamda expression implementation
        FunInter funInter3 = (String name) ->{
            System.out.println("I am implementing the override method in the class:"+name);
        };
Copy the code
  • Overall code:

public class NoReturnMultiParam {
    public static void main(String[] args) {
        //1. The normal implementation of the interface
        FunInterImpl funInter = new FunInterImpl();
        funInter.method("1. Implementation class");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        //2. Static inner class implementation
        FunInterImpl funInter1 = new FunInterImpl();
        funInter.method("2. Static inner classes");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        //3. Local inner class
        class FunInterImpl2 implements FunInter{
            @Override
            public void method(String name) {
                System.out.println("I am implementing the override method in the class:"+name);
            }
        }
        FunInterImpl2 funInterImpl2 = new FunInterImpl2();
        funInterImpl2.method("3. Local inner class");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        //4. Anonymous inner class, want a semicolon; At the end
        FunInter funInter2 = new FunInter() {
            @Override
            public void method(String name) {
                System.out.println("I am implementing the override method in the class:"+name); }}; funInter2.method("4. Anonymous Inner classes");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        //5. Lamda expression implementation
        FunInter funInter3 = (String name) ->{
            System.out.println("I am implementing the override method in the class:"+name);
        };
        funInter3.method("5. Lamda expression implementation");
    }

    //2. Static inner class implementation
    static class FunInterImpl implements FunInter{
        @Override
        public void method(String name) {
            System.out.println("I am implementing the override method in the class:"+name); }}}/** * this annotation is used to mark a functional interface * 1. An annotated interface can have only one abstract method * 3. An annotated interface can have default/static methods, or methods that override Object */
@FunctionalInterface
interface FunInter{
    void method(String name);
}

/** * 1. Normal interface implementation class */
class FunInterImpl implements FunInter{

    // Override this method
    @Override
    public void method(String name) {
        System.out.println("I am implementing the override method in the class:"+name); }}Copy the code
  • The execution result is as follows:

2. Lamda expression
  • Implementation method:

public class lamdaTest01 {
    public static void main(String[] args) {

        /**
         * 多参数无返回
         */
        NoReturnMultiParam1 n1 = (int a,int b) ->{
            System.out.println("Multi-parameter no return :"+a+b);
        };
        n1.method(1.2);

        /** * No parameter no return value */
        NoReturnNoParam n2 = ()->{
            System.out.println("No parameters, no return value"); }; n2.method();/** * One parameter is not returned */
        NoReturnOneParam n3 = (int a) -> {
            System.out.println("One parameter is not returned" + a);
        };
        n3.method(11);

        /** * Multiple arguments have a return value */
        ReturnMultiParam n4 = (int a,int b)->{
            System.out.println("Multiple arguments have return values" + a);
            return 1;
        };
        System.out.print("* * * *"+n4.method(1.3));

        /** * Returns */ if there is no parameter
         ReturnNoParam n5 = ()->{
             System.out.println("Return without a parameter.");
             return 1;
         };
        System.out.print("* * *"+n5.method());

        /** * A parameter has a return value */
        ReturnOneParam n6 = (int a) ->{
            System.out.println("A parameter has a return value."+a);
            return a;
        };
        System.out.print("* * *"+n6.method(1)); }}/** Multiple parameters none */
@FunctionalInterface
interface NoReturnMultiParam1 {
    void method(int a, int b);
}

/** No parameter no return value */
@FunctionalInterface
interface NoReturnNoParam {
    void method(a);
}

/** One parameter is not returned */
@FunctionalInterface
interface NoReturnOneParam {
    void method(int a);
}

/** Multiple arguments have a return value */
@FunctionalInterface
interface ReturnMultiParam {
    int method(int a, int b);
}

/*** Returns */ if there is no parameter
@FunctionalInterface
interface ReturnNoParam {
    int method(a);
}

/** A parameter has a return value */
@FunctionalInterface
interface ReturnOneParam {
    int method(int a);
}
Copy the code

The execution result is as follows:

3. Simplification of LAMDA expression:
  • Ways to simplify:
		 //1. Simplify the parameter type. You can not write the parameter type, but all parameters must not be written
        NoReturnMultiParam1 lamdba1 = (a, b) -> {
            System.out.println("Simplified parameter types");
        };
        lamdba1.method(1.2);

        //2. Simplify the argument parentheses. If there is only one argument, the argument parentheses can be omitted
        NoReturnOneParam lambda2 = a -> {
            System.out.println("Simplified parameter braces");
        };
        lambda2.method(1);

        //3. Simplify the method body curly braces. If the method bar has only one statement, you can win the method body curly braces
        NoReturnNoParam lambda3 = () -> System.out.println("Simplified method body curly braces");
        lambda3.method();

        //4. If the method body contains only one statement and the statement is a return statement, you can omit the braces
        ReturnOneParam lambda4 = a -> a+3;
        System.out.println(lambda4.method(5));

        ReturnMultiParam lambda5 = (a, b) -> a+b;
        System.out.println(lambda5.method(1.1));
Copy the code

The execution result is as follows:

  • conclusion
    • Simplified parameter types. Parameter types may not be written, but none of the parameters must be written
    • Simplify the argument parentheses by omitting them if there is only one argument
    • Simplified method body braces. If the method bar has only one statement, you can win the method body braces
    • If the method body has only one statement and is a return statement, the method body curly braces can be omitted

I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah