- Lambda expressions are anonymous method(methods without names) that implement abstract function of functional interfaces.
- An interface with single abstract method is called functional interface. Example: Runnable interface with run() method.
- Arraow Operator
- Arrow operator divides lambda expression into 2 parts
- (n) -> n*n;
- The left side specifies the parameters required by the expression, which could also be empty if no parameters are required.
- The right side is the lambda body which specifies the actions of the lambda expression. It might be helpful to think about this operator as “becomes”. For example, “n becomes n*n”, or “n becomes n squared”.
interface NumericTest { boolean computeTest(int n); } public static void main(String args[]) { NumericTest isEven = (n) -> (n % 2) == 0; NumericTest isNegative = (n) -> (n < 0); // Output: false System.out.println(isEven.computeTest(5)); // Output: true System.out.println(isNegative.computeTest(-5)); }