Java 8 : Functional Interfaces

Spread the love

Functional interface in Java 8 is a special kind of interface which contains only one abstract method. Some common examples of such interfaces are Runnable, Comparable etc.

It cannot have more than one abstract method.

The idea to use functional interfaces is using it with Lambda Expressions. Let’s go through an example involving the Runnable Interface.



public class LambdaExpressionThreadExample{  
    public static void main(String[] args) {  
      
        //Without Lambda Expression
        Runnable r1=new Runnable(){  
            public void run(){  
                System.out.println("Thread No 1 is running");  
            }  
        };  

        Thread t1=new Thread(r1);  
        t1.start();  

        //With lambda Expression
        Runnable r2=()->{  
                System.out.println("Thread No 2 is running");  
        };  

        Thread t2=new Thread(r2);  
        t2.start();  
    }  
}  

Here we are creating a Thread and Starting a thread using the traditional way and using Lambda Expressions.

If you see the one with Lambda Expression, it doesn’t have any run method as such. It just has a statement which acts as an Anonymous Function. It works as an implementation of the abstract method run which is part of the Functional Interface Runnable.

So Lambda Expressions are quite easy to work with.

We can also use the @FunctionalInterface annotation with an interface to denote that it is a Functional Interface. Let’s Look at Another Example.

import java.util.*;

@FunctionalInterface
interface Add{
    
    void getSum(int x,int y);
}


public class LambdaExAddEg{  
    public static void main(String[] args) {  
      
        Add a = (x,y)->{System.out.println(x+y);};//line 2
        a.getSum(6,5);//line 3
    }  
}  

Here we used an Functional Interface called Add with Lambda Expression to print the sum of 2 numbers.

The statement ” System.out.println(x+y); ” inside the curly braces is actually the definition of the abstract method getSum of the interface Add.

At line 3, the call is made and the output was printed.

Some new Functional Interfaces were introduced in Java 8 like below:

-> Predicate: It accepts a single argument and returns either true or false. It has the method test in it. Filter function in Java 8 accepts the implementation of the predicate interface as below.

List<Integer> nums= Arrays.asList(50,70,90,100,120);

nums.stream().filter(n -> n > 75)

It will filter all numbers greater than 75 in the list.

->Consumer: It accepts a single argument and doesn’t return anything. It only consumes. It has the accept method.

        nums.stream().forEach(  
                n -> System.out.println(n)  
        );

ForEach in Lambda Expression accepts the Consumer Implementation. The above code is consuming the numbers and displaying it using Sys Out.

-> Supplier: A Supplier is a simple interface which indicates that this implementation is a supplier of results. It contains the get() method.

Supplier<Double> doubleSupplier = () -> Math.random();

-> Function: A Function interface is more of a generic one that takes one argument and produces a result. It accepts argument T and Produces result R. It has a method named accept.

Hope the tutorial was clear. Thanks for reading.