LambdaExpression - shinichikudu10/learning GitHub Wiki
Lambda Expression
Functional Interfaces
1. What is functional Interface?:
- Kind of Interface but only 1 abstract method;
2. How to use:
Ex1: Correctly
```
package lambda.expression.demo;
@FunctionalInterface
public interface DemoFunctionalInterface {
public void sayHello(String helloMessage);
}
```
Ex2: Incorrect
```
package lambda.expression.demo;
@FunctionalInterface
public interface DemoFunctionalInterface {
}
```
Or
```
package lambda.expression.demo;
@FunctionalInterface
public interface DemoFunctionalInterface {
void doSomethingA();
void doSomethingB();
}
```
1.3 Functional can have methods in Java.lang.Object
Ex3:
```
package lambda.expression.demo;
@FunctionalInterface
public interface DemoFunctionalInterface2 {
void doSomething();
// medthod in Java.lang.Object
int hashCode();
String toString();
boolean equals(Object obj);
}
```
Interface below is correctly?
Ex4:
```
package lambda.expression.demo;
@FunctionalInterface
public interface DemoFunctionalInterface2 {
int hashCode();
String toString();
boolean equals(Object obj);
}
```
=> Because it only declared from method of Java.lang.Object, Not exist abstract method.
Ex5:
```
package lambda.expression.demo;
@FunctionalInterface
public interface DemoFunctionalInterface2 {
void doSomething();
Object clone();
}
```
=> Function clone of class Object is protected, so here isn't override of method clone() in
Object class. So we have 2 method in this interface are doSomething() and clone()
Ex6:
```
@FunctionalInterface
public interface DemoFunctionalInterface2 {
default void test1() {
System.out.println("Hello");
}
default void test2() {
System.out.println("Hello");
}
static void test3() {
System.out.println("Hello");
}
}
```
Ex7: Functional Interface extends others Interfaces
```
package lambda.expression.demo;
interface BaseInterface1 {
void base();
}
interface BaseInterface2 {
void base();
}
@FunctionalInterface
public interface DemoFunctionalInterfaceExtend extends BaseInterface1, BaseInterface2 {
// void dummy();
}
```
=> Functional interface only can use when not exist any abstract method in Functional interface
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
Lambda Expression
From Java 8, new operator *->* added
- Syntax: *(argument-list) -> {body}*
- Argument-list: List parameters.
Where lambda operator can be:
Zero parameter:
() -> System.out.println("Zero parameter lambda");
One parameter:–
(p) -> System.out.println("One parameter: " + p);
It is not mandatory to use parentheses, if the type of that variable can be inferred from the context
Multiple parameters :
(p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2);
- Arrow-operator: toán tử mũi tiên được sử dụng để liên kết danh sách tham số và body của
biểu thức.
- Body: Body of lambda Expression (Expression,
Ex:
```
package lambda.expression.demo;
@FunctionalInterface
interface LambdaExpressionSyntax {
// An abstract function
void abstractMultiply(int x);
// A non-abstract (or default) function
default void normalFun()
{
System.out.println("Hello");
}
}
package lambda.expression.demo;
public class LambdaExpressionTest {
public static void main(String[] args) {
LambdaExpressionSyntax obj = (int x) -> System.out.println(2*x);
obj.abstractMultiply(20);
}
}
```
- Rules:
Correct: (int a, int b, int n) -> { doSomthing(); }
Incorrect: (int a; int b; int n) -> { doSomthing(); }