Agenda
2.Supplier
3.Function
4.Predicate
A function having no
1. Name
2. Return type
3. Access Modifiers
Let's understand the above concept with the help of an example.
Take a method having name m return type int and access modifier public
public int m(){};
Note here; is optional
Now apply the Lambda Expression to the above method.
public int m(){};
means remove
1. Name i.e m
2. Return type i.e int
3.Access Modifiers i.e public
After removing it becomes
() {}
To make it Lambda express we have a link with arrow symbol -> like below.
()->{};
Note here; is mandatory.
Important points to Remember
Example1
public int add(int a,int b) {
return a+b;
}
(int a,int b)->{return a+b};//valid
(a,b)->{return a+b};//valid
(a,b)->return a+b;//invalid
(a,b)->a+b;//valid
a,b->{return a+b;};//invalid
Example2
public void method(int a){}
(int a)->{System.out.println(a);};
(a)->{System.out.println(a);};
a->{System.out.println(a);};
a->System.out.println(a);
Example3
public void methodOne(){}
()->{System.out.println("Anu");};
()->System.out.println("Anu");
Now think how can you invoke the above lambda express.
The answer is with the help of Functional Interface.
Functional Interface.
Functional Interfaces are those which satisfied the below rules.
1. Having only one abstract method.
2. No restriction with the default method
3. No restriction with the static method.
For example
InterF f=()->{System.out.println("hello");};
f.m();
Example
@FunctionalInterface
public interface InterF {
public void m1();
public void m2();
}//you will get compilation error.
If an interface extends Functional Interface and child interface doesn't contain any abstract method then the child interface is also a Functional Interface.
@FunctionalInterface
public interface InterA {
public void m1();
}
@FunctionalInterface
public interface InterB extends InterA {
public void m1();
}//No compilation error.
@FunctionalInterface
public interface InterB extends InterA {
public void m2();//compilation error
}
public interface InterB extends InterA {
public void m2();//No compilation error because this is not a functional interface. it is a normal interface which can contain any number of abstract methods
}
Happy coding.
You can download the code from Github using the below URL.
https://github.com/mahendrakr/java8
- Lambda Expression
- Functional Interface
- Default Method
- Inbuilt functional interfaces
2.Supplier
3.Function
4.Predicate
- Double colon operator(::)
- Stream API
- Optional
- Date and Time API
A function having no
1. Name
2. Return type
3. Access Modifiers
Let's understand the above concept with the help of an example.
Take a method having name m return type int and access modifier public
public int m(){};
Note here; is optional
Now apply the Lambda Expression to the above method.
public int m(){};
means remove
1. Name i.e m
2. Return type i.e int
3.Access Modifiers i.e public
After removing it becomes
() {}
To make it Lambda express we have a link with arrow symbol -> like below.
()->{};
Note here; is mandatory.
Important points to Remember
- The method parameter type is optional, Compiler automatically knows the method parameter type which is called Type Inference. You know Functional Interface contains only one abstract method so the compiler can guess the type of parameter.
- If there is only one parameter then parenthesis () is also optional.
- If there is only one statement inside the method body then curly braces also optional. But if you removing {} then also remove return keyword otherwise
- you will get a compilation error.
- If there is no parameter then parenthesis is mandatory.
- If there are more than one parameters then also parenthesis is mandatory.
Example1
public int add(int a,int b) {
return a+b;
}
(int a,int b)->{return a+b};//valid
(a,b)->{return a+b};//valid
(a,b)->return a+b;//invalid
(a,b)->a+b;//valid
a,b->{return a+b;};//invalid
Example2
public void method(int a){}
(int a)->{System.out.println(a);};
(a)->{System.out.println(a);};
a->{System.out.println(a);};
a->System.out.println(a);
Example3
public void methodOne(){}
()->{System.out.println("Anu");};
()->System.out.println("Anu");
Now think how can you invoke the above lambda express.
The answer is with the help of Functional Interface.
Functional Interface.
Functional Interfaces are those which satisfied the below rules.
1. Having only one abstract method.
2. No restriction with the default method
3. No restriction with the static method.
For example
- public interface InterF {
- public void m();
- default void m2() {
- }
- default int m3() {
- return 0;
- }
- public static void m4() {
- }
- public static void m5() {
- }
- }
InterF f=()->{System.out.println("hello");};
f.m();
- public class Test {
- public void m6() {};
- public static void main(String[] args) {
- InterF f=()->{System.out.println("Hello Lambda");};
- f.m();
- }
- }
Example
@FunctionalInterface
public interface InterF {
public void m1();
public void m2();
}//you will get compilation error.
If an interface extends Functional Interface and child interface doesn't contain any abstract method then the child interface is also a Functional Interface.
@FunctionalInterface
public interface InterA {
public void m1();
}
@FunctionalInterface
public interface InterB extends InterA {
public void m1();
}//No compilation error.
@FunctionalInterface
public interface InterB extends InterA {
public void m2();//compilation error
}
public interface InterB extends InterA {
public void m2();//No compilation error because this is not a functional interface. it is a normal interface which can contain any number of abstract methods
}
Happy coding.
You can download the code from Github using the below URL.
https://github.com/mahendrakr/java8