Java8 Lambda Fuctional Interface



Fuctional Interface

Interface that have only one method is known as Fuctional Interface.
Let's run some Runnable! With Java 8. It is used to create an instance of Runnable by implementing its run method. This is possible only for interfaces that have only one abstract method, and such interfaces are called functional interfaces.

without Lambda
Runnable r = new Runnable() {
        @Override
        public void run() {
           System.out.println("javat");
        }
};

r.run();

with Lambda
Runnable r = () -> System.out.println("javat");

r.run();


*Fuctional Interface didn't allow more than one method declaration.
*Fuctional Interface only allow single method declaration.




Creating Program in Java8(Lambda) unserDefined Interface


Follow the below steps to write program with java8(Lambda).
Step-1

Create a package com.javat.functionalInterface under sources package. Then create the Interface PersonalId.java. Once you are ready then write the code as shown below.




Step-2

Create the Java class FunctionalinterfaceDemo.java. Once you are ready then write the code as shown below.




Output

FunctionalinterfaceDemo.java



Creating Program in Java8(Lambda) PreDefined Interface

Follow the below steps to write program with java8(Lambda).
Step-1

Create a package “com.javat.functionalInterface” under sources package-> then create the Class “ThreadFunctionalInterface.java”. Once you ready then write below code.




Output

ThreadFunctionalInterface.java







Download example and run it on Netbeans IDE