Pages

Java 8

Agenda
               1.Consumer
               2.Supplier
               3.Function
               4.Predicate
Lambda Expression a.k.a an Anonymous Function.
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
  1. 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.
  2. If there is only one parameter then parenthesis () is also optional.
  3. 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
  4. you will get a compilation error.
  5. If there is no parameter then parenthesis is mandatory.
  6. If there are more than one parameters then also parenthesis is mandatory.
Convert below methods in lamda expression
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
  1. public interface InterF {
  2.     public void m();
  3.     default void m2() {
  4.     }
  5.     default int m3() {
  6.         return 0;
  7.     }
  8.     public static void m4() {
  9.     }
  10.     public static void m5() {
  11.     }
  12. }
How to invoke the lambda express  with the help of Functional Interface:
   InterF f=()->{System.out.println("hello");};
   f.m();
  1. public class Test {
  2.   public void m6() {};
  3.     public static void main(String[] args) {
  4.        InterF f=()->{System.out.println("Hello Lambda");};
  5.     f.m();
  6.     }
  7. }
Note: In Java 1.8v @FunctionalInterface annotation is introduced to specify that the interface is Functional interface and if you not abide functional interface rules then you will get compilation error.

Example
@FunctionalInterface
public interface InterF {
    public void m1();
    public void m2();
}//you will get compilation error.
    Functional Interface w.r.t Inheritance
    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