as we've already mentioned in the previous blog, lambda expressions allow for more concise code to create an instance of an interface with only one abstract method. Now let's write a Java command-mode to study the syntax of the lambda expression itself.
here Repeat the command mode:
Consider a scenario where a method needs to complete an action, but the concrete implementation of the behavior cannot be determined and must wait until the method is executed. For example, I have a way to iterate through an array element of an array, but I cannot determine how to handle the elements when traversing the array element, and you need to specify the specific handling behavior when the method is actually invoked. That means I'm going to deal with some parameters now, but I'm not sure how to handle these parameters, I want to write a method that can pass in some parameters, or pass in the processing of these parameters, how to do it. Use the command mode because Java is not allowed to pass a code into a method before java8.
here is the original code:
public class Test
{
//handle array public
void process (int[] array, Command cmd)
{
cmd.process (array);
Public
static void Main (string[] args)
{
test test = new test ();
Int[] target = {1, 2, 3};
Test.process (Target, New Command ()
{
@Override public
void process (int[] array)
{for
(int i : target)
{
System.out.println (i);
}}}
);
}
Interface Command
{
//handling array behavior
void process (int[] array);
Now we use the lambda expression to change the above code:
public class Test
{
//handle array public
void process (int[] array, Command cmd)
{
cmd.process (array);
} Public
static void Main (string[] args)
{
Test test = new test ();
Int[] target = {1, 2, 3};
Test.process (Target, (int[] array)->
{for
(int i:target)
{
System.out.println (i);
}
});
}
}
@FunctionalInterface
interface Command
{
//handle array's behavior
void process (int[] array);
from the code above, we can see that we don't need to write the cumbersome code for new XXX () {} By using lambda expressions, and we don't need to point out the name of the overriding method, nor do we need to give the return value type of the overridden method. You can just give the overridden method brackets and the list of parameters inside the parentheses. In fact, the main function of lambda expressions is to replace the tedious syntax of anonymous inner classes.
lambda expressions are composed of three parts:
1), the formal parameter list. The parameter list allows you to omit the parameter type, and if there is only one parameter, even the parentheses of the new parameter list can be omitted.
2), arrow. ->.
3), code block. If the code block contains only one statement, you can omit the curly braces. If the code block has only one return statement, you can omit the return keyword. If a lambda expression needs to return a value, and only one of its code blocks has a statement that omits returns, the lambda expression automatically returns the value of the statement.
The following is the actual encoding to demonstrate the syntax of the following lambda expression:
public class Test {public void Testa (a) {System.out.println (a);
A.test ();
public void Testb (b b) {System.out.println (b);
B.test ("111");
public void Testc (c c) {System.out.println (c);
System.out.println ("Here is the expression of interface C:" + c.test (1, 2));
public static void Main (string[] args) {test test = new test ();
Test.testa (()-> {System.out.println ("Here is the lambda expression of interface a)";
});
The code block above has only one line of code that can be abbreviated Test.testa (()-> System.out.println ("Here is the lambda expression of interface A)");
TEST.TESTB (str)-> System.out.println ("Here is the lambda expression of interface B + str");
There is only one parameter argument above, which can be abbreviated TEST.TESTB (str-> System.out.println ("Here is the lambda expression of interface B" + str));
TEST.TESTC ((A, B)-> {return (A + B);
});
The code block above has only one line of code, you can omit curly braces, or you can omit the return statement TEST.TESTC ((A, B)-> A + B);
}} @FunctionalInterface interface A {void Test ();}
@FunctionalInterface interface B {void Test (String str);} @FunctionalInterface interface C {int. test (int A, int b);}
the code here can be compiled and run normally, stating that a lambda expression is actually going to be treated as an object of any type, depending on what type of object it needs to be in the running environment. Next blog I will be organized into functional interface, the time will be clear.
in the above use process, we saw:
1, when we use lambda expressions, the type of the argument can be deduced, and of course we can add the type of the argument or the annotation or other modifiers ourselves. If you need to explicitly specify the type of a parameter, then you must declare the type for all parameters. For example:
(int A, int b)-> (a%b) ==0 is legal
(int A, b)-> (a%b) ==0 is illegal
2, you never need to perform a return type for a lambda expression, which can always be inferred from the context. For example:
(String first,string second)->integer.compare (First.length (), second.length ()); can be used in a context where the expected result type is int.
3, in a lambda expression, only the values are returned in some branches, and it is illegal for other branches to return no values. For example, the following code:
public class Test
{public
static void Main (string[] args)
{
A A = (b)->
{
if (b > 0)
{ return
1;}}
;
} @FunctionalInterface
Interface a
{
int a);
}
4), the type of the abstract method and the type of the expression must be compatible in order to use the lambda expression in the context of the target type. Specifically, the type and number of arguments for a lambda expression must be compatible with the parameters of the method, the return type must be compatible, and the exception that the lambda expression might throw must be accepted by the method.
5), the method body of the lambda expression has the same scope as the nested code block. So he also applies the same naming conflict and masking rules. Declaring a parameter or local variable with the same name as a local variable is not allowed in a lambda expression. For example, the following code error:
int A;
TEST.TESTC ((A, b)->a + B);