1. Overview

In this article, we will learn what is functional interface with lambda expression java 8.

2. Development environment

Java            : Oracle JDK 1.8
IDE              : IntelliJ

3. What is Functional Interface ?

Function Interface is a interface which contain only one abstarct method and any number of static and default methods. Java 8 supports Functional Interface (FI) with Lambda Expression which helps us to remove the boiler-plate code.

We can define only abstract methods inside interface till java 1.7.
java 8 expands the interface feature such that we can have a default or static methods inside interface.

In order to declare a functional interface, we have to use @FunctionalInterface  annotation.
When we define an interface using,@FunctionalInterface we must have to define at least one abstract method.
@FunctionalInterface is not mandatory to use while defining the functional interface.

Functional Interface without using Annotation Example

interface Addition{

public void add(int a,int b);

}

Functional Interface using Annotation Example

@FunctionalInterface 
interface Addition{

public void add(int a,int b);

}

4. What is Lambda Expression?

Java 8 is extended to have Functional Programming feature
i.e Lambda Expression.

Lambda Expression is an anonymous function and does not have any return type, access modifier and without belonging to any class.
Lambda Expressions syntax (number of argument) -> (body contain)

Lambda expression examples

(a,b) -> System.out.println(“Sum of ”+(a+b);
(int n) -> {return n*n;)
(int n)-> n*n;
(n) - > n*n;

Lambda expression rules

Without curly braces, we can not use return keyword because the compiler will consider return value automatically.
Within curly braces, if we want to return any value, we have to use the return statement.
Lambda expression used for Function Interface (FI) should contain only single abstract method.

5. How to write lambda expression with Functional Interface?

In following example, we will demonstrate functional interface without using lambda expression in Java 7.

@FunctionalInterface
interface Addition{
    public int add(int a,int b);
}
class Operation implements Addition{
@Override
    public int add(int a, int b) {
        return a+b;
    }
}
class MainApp {
    public static void main(String[] args) {
     Operation ope = new Operation() ;
     System.out.println("Sum : "+ope.add(10,20));
   }
}

In above example Operation class implements Addition functional interface and override add method to return the sum of two numbers.

Now, we will demonstrate the use of lambda expression using Java 8.

@FunctionalInterface
interface Addition{
    public int add(int a,int b);
}
class MainApp {
    public static void main(String[] args) {
        Addition addition = (a,b) -> a+b;
        System.out.println("Sum : "+addition.add(10,20));
    }
}

Both examples that we demonstrated above produces the same output but functional interface with lambda expression java 8 is having less boiler-plate code.

References

Oracle Java 8 Docs

 

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *