Java Syntactic Sugar--The Meaning of Lambda Expression

1. The essence of lambda expressions

Lambda expressions and anonymous inner classes are actually used to generate an instance of an interface.

Compared with implementing an interface through a class and then instantiating an object by the class, Lambda expressions and anonymous inner class syntax are more concise, and you can generate an instance of the interface without having to define the class.

2. Grammar

For example, the encryption interface is as follows:

/**
 * 加密接口
 */
public interface IEncoder {
    
    
	/**
	 * 对数字加密
	 */
	public int encode(int num);
}

2.1 Encryption through classes

To implement a specific encryption algorithm, the most common way is to define a class to implement an interface. The following code is purely a normal operation and does not need to be explained.

/**
 * 加法加密
 */
public class AddEncoder implements IEncoder {
    
    
	@Override
	public int encode(int num) {
    
    
		return num + 1;
	}
	/**
	 * 测试
	 */
	public static void main(String[] args) {
    
    
		IEncoder encoder = new AddEncoder();
		int result = encoder.encode(1);
		System.out.println("加密结果:" + result);
	}
}

2.2 Encryption through anonymous inner classes

If an anonymous inner class is used, there is no need to specifically define a class, just define an anonymous inner class when needed.

public class AnonymousDemo {
    
    
	public static void main(String[] args) {
    
    
		// 使用时临时定义类
		IEncoder encoder = new IEncoder() {
    
    
			@Override
			public int encode(int num) {
    
    
				return num + 1;
			}
		};
		int result = encoder.encode(1);
		System.out.println("加密结果:" + result);
	}
}

For the encoder objects in the above two examples, their capabilities are actually the same, and they are both instances with additive encryption methods. However, in terms of the specific method of generating this object, the anonymous inner class can be used temporarily to customize the encryption method, which is more lightweight than specifically defining a class.

2.3 Encryption through Lambda expressions

Lambda expressions are simpler, but essentially they still generate an interface instance.

public class LambdaDemo {
    
    
	public static void main(String[] args) {
    
    
		// 通过表达式定义接口的实力
		IEncoder encoder = (num) -> {
    
    
			return num + 1;
		};
		int result = encoder.encode(1);
		System.out.println("加密结果:" + result);
	}
}

As you can see, in fact, Lambda expressions are still showing the concrete realization of a method.

  • (num)Represents parameters
  • ->Indicates that this is a Lambda expression
  • {return num+1;}Code block, write specific method logic.

2.4 Shorthand for Lambda expression

When the parameter list has only one parameter, the parentheses of the parameter can be omitted.

When there is only one statement in the code block, the code block can be omitted directly {}.

When there is only one code block return, it can also be omitted.

So the above expression can be simplified to:

public class LambdaDemo {
    
    
   public static void main(String[] args) {
    
    
   	// 通过表达式定义接口的实力
   	IEncoder encoder = num -> num + 1;
   	int result = encoder.encode(1);
   	System.out.println("加密结果:" + result);
   }
}

3. The meaning of Lambda expression

Anonymous inner classes and Lambda expressions are actually to simplify the code of interface instantiation.

When the instance of the interface is only used once, it is a bit wasteful to write a class alone, it is better to get it done directly with an expression.

Lambda is simpler than anonymous inner classes, but Lambda expressions can only face interfaces with only one method, while anonymous functions can support interfaces with any number of methods

4. Functional Interface

Lambda expressions can support only one method interface, which can be called a functional interface.

Java8 provides an annotation for functional interfaces @FunctionalInterfaceto check whether the interface is functional. This shows the importance of functional interfaces!

The syntax is as follows:

/**
 * 加密接口
 */
@FunctionalInterface
public interface IEncoder {
    
    
	/**
	 * 对数字加密
	 */
	public int encode(int num);
}

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/107591447