如下面的代码:
interface A {
void f1();
void f2(int x, int y);
}
A a1 = ()->xxx;
A a2 = (x, y)->xxx;
-
Important : The functional interface also known as Single Abstract Method Interface was introduced to facilitate Lambda functions. Since a lambda function can only provide the implementation for 1 method it is mandatory for the functional interface to have ONLY one abstract method.
-
Writing Lambda expression meaning we are implementing the interface that is functional interface. It should have one abstract method because at the time of lambda expression, we can provide only one implementation at once. So in the code snippet posted in the question, at any time we are giving only one implementation while declaring Lambda where we will have to implement for two abstract methods.
Matching Lambdas to Interfaces
A single method interface is also sometimes referred to as a functional interface. Matching a Java lambda expression against a functional interface is divided into these steps:
-
Does the interface have only one abstract (unimplemented) method?
-
Does the parameters of the lambda expression match the parameters of the single method?
-
Does the return type of the lambda expression match the return type of the single method?
If the answer is yes to these three questions, then the given lambda expression is matched successfully against the interface.