In this shot, we discuss how to create a functional interface in Java.
An interface that only consists of one abstract method is called a functional interface.
Lambda expressions can be used to denote the instance of a functional interface.
(String s, float f) -> {System.out.println(“Lambda Expression having ”+s+”and”+f);}
In the example above:
(String s, float f)
is an argument list.->
is the arrow token.{System.out.println(“Lambda Expression having ”+s+”and”+f);}
is the body of the lambda expression.We can approach this problem by creating our own functional interface.
Then, in the main function of the class, we create an instance for the functional interface.
Now, we display something using the defined functional interface and lambda expression.
Let’s take a look at the code.
interface funInterface{public int product(int a, int b);}class Main{public static void main(String[] args){funInterface multiply = (a,b) -> a*b;System.out.println("Multiplication result is: " +multiply.product(190,30));}}
int
type parameters.Main
class that has the main
function inside it.This is how we can create a functional interface using lambda expressions in Java.