Blog Detail

  • Home
  • Lambda with Functional Interface in Java

Lambda with Functional Interface in Java

Advantages of lambda

  • Reduce the number of lines
  • Easily define functional interface
  • return statement and parenthesis are optional

Functional Interface

The interface which is annotated with @FunctionalInterface is known as Functional interface. the best example of functional interface is Runnable, Cloneable, Callable

package com.samplecoder.lambda;

public class LambdaWithFunctionalInterface {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Runnable runnable1 = () -> {
			System.out.println("This is runnable interface 1");
		};
		
		
		Thread thread1 = new Thread(runnable1);
		
		Thread thread2 = new Thread(() -> {
			System.out.println("This is runnable interface 2");
		});
		
		thread1.start();
		
		thread2.start();
	}

}

Result

This is runnable interface 1
This is runnable interface 2

Write a comment