Initial Lambda expression

Posted by er0x on Sat, 14 Sep 2019 15:54:56 +0200

Article directory

Lambda Basic Grammar

The basic syntax of Lambda expressions: Java 8 introduces a new operator "->", which is called the arrow operator or Lambda operator. The arrow operator splits the Lambda expression into the following two parts:
1. Left side: parameter list of Lambda expression. For example, there is an interface called public Boolean test (T); then the parameter list is a T-type object.
2. Right: The function that needs to be performed in Lambda expression, namely Lambda body

Grammar Format 1: No Parameters, No Return Values
() -> System.out.printIn("Hello Lambda!");
Examples are as follows:

public class TestLambda{
	@Test
	public void test1(){
// Do not use Lambda expressions
	Runnable r = new Runnable(){
		@Override
		public void run(){
			System.out.printIn("Hello World");
}
};
	r.run();
// Using Lambda expressions
Runnable r1 = () -> System.out.printIn("Hello World");
r1.run();
}
}

Grammar Format 2: There is one parameter with no return value. If there is only one parameter, parentheses of parameters can be omitted and not written.
(x) -> System.out.printIn(x);
The parameter name is arbitrary.
Examples are as follows:

public void test2(){
Consumer<String> con = (x) -> System.out.printIn(x);
con.accept("Study hard.);
} 

Syntax Format 3: There are two or more parameters with return values and multiple statements in the Lambda body
(x,y) -> {
System.out.printIn("functional interface");
return Integer.compare(x,y);
};
If there are multiple statements in the Lambda body, braces and semicolons must be used.
Examples are as follows:

public void test4(){
	Comparator<Integer> com = (x,y) -> {
			System.out.printIn("Functional Interface");
			return Integer.compare(x,y);
};
}

Grammar Format 4: There are two or more parameters with return values and only one statement in Lambda body, so return and braces can be omitted.
(x,y) -> Integer.compare(x,y);

Examples are as follows:

public void test4(){
	Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
}

Grammar Format 5: The parameter types of Lambda expressions can be omitted. Because JVM compilers infer data types through context, that is, "type inference".
(Integer x,Integer y) -> Integer.compare(x,y);
Examples are as follows:

public void test5(){
	Comparator<Integer> com = (Integer x,Integer y) -> Integer.compare(x,y);
}

Lambda expressions require the support of "functional interfaces"

Functional interface: There is only one abstract method in the interface, called functional interface. You can use the annotation @Functional Interface modifier to check if the interface is a functional interface.
Examples are as follows:

@FunctionalInterface
public interface MyInterface<T> {
	public boolean test(T t);
}

Explanation: If you modify the interface with @Functional Interface, it means that it can only be a functional interface, then there is only one abstract method in the interface, and if you define more than one method, you will report an error.

Examples of Lambda expressions

Step 1: Customize a functional interface;
@FunctionalInterface
public Interface MyFun<T> {
	public Integer getValue(Integer num);
}
//Step 2: Customize a function method; (This method can be written in a class with the call method below)
public integer operation(Integer num,MyFun mf){
	return mf.getValue(num);
}
//Step 3: Call the operator method above.
public void test6(){
	// Do a multiplication operation
	Integer num = operation(100,(x) -> x*x);
	System.out.printIn(num);
	// Do an addition operation
	Integer numAdd = operation(100,(x) -> x+200);
	System.out.printIn(numAdd);

}

Topics: Lambda Java jvm