Lambda Expression

by Santosh K Saha | Category Java-8


A Lambda expression is used to create anonymous method. Lambda expression is a block of code or a behavior that gets passed around in a concise way, like an anonymous method. Lambda expressions are a convenient way to create anonymous functions.

Lambda Syntax
(Parameter) -> { body }

Lets understand the lambda expression with examples


Function interface with method that takes no parameter and returns void

Function Descriptor : () -> void

@FunctionalInterface
public interface FunctionalInterfaceOne {
	
	public void abc();  // ()->void
	
}

Function interface with method that takes String as a parameter and returns void

Function Descriptor : (String) -> void

@FunctionalInterface
public interface FunctionalInterfaceTwo {

	public void xyz(String name);  // (String)->void
	
}

Function interface with method that takes two long as a parameter and returns double

Function Descriptor : (long, long) -> double

@FunctionalInterface
public interface FunctionalInterfaceThree {

	public double mno(long i, long j);  // (long, long)->double
	
}

Example
public class App {

	public static void main(String[] args) {

		// ----------- Using Anonymous Class -----------------------
		FunctionalInterfaceOne a1 = new FunctionalInterfaceOne() {

			@Override
			public void abc() {
				System.out.println("Method takes no parameter and returns void");
			}
		};

		FunctionalInterfaceTwo a2 = new FunctionalInterfaceTwo() {

			@Override
			public void xyz(String name) {
				System.out.println("name -->> " + name);
				System.out.println("Method takes String as a parameter and returns void");
			}
		};

		FunctionalInterfaceThree a3 = new FunctionalInterfaceThree() {

			@Override
			public double mno(long i, long j) {
				System.out.println("i -->> " + i + ", j -->> " + j);
				System.out.println("Method takes two long as a parameter and returns double");
				return 0;
			}
		};

		// ----------- Using Lambda Expression -----------------------
		FunctionalInterfaceOne l1 = () -> System.out.println("It takes no parameter and returns void");
		
		FunctionalInterfaceTwo l2 = (name) -> {
			System.out.println("name -->> " + name);
			System.out.println("It takes String as a parameter and returns void");
		};

		FunctionalInterfaceThree l3 = (i, j) -> {
			System.out.println("i -->> " + i + ", j -->> " + j);
			System.out.println("It takes  two long as a parameter and returns double");
			return i + j;
		};

		System.out.println("============Uses anonymous class================");
		a1.abc();
		a2.xyz("Ram");
		a3.mno(5, 6);
		
		System.out.println("============Uses lambda expression================");
		l1.abc();
		l2.xyz("Hari");
		l3.mno(6, 7);
	}

}
Output :
============Using Anonymius Class================
Method that takes that takes no parameter and returns void
name -->> Ram
Method that takes that takes String as a parameter and returns void
i -->> 5, j -->> 6
Method that takes that takes two long as a parameter and returns double
============Using Lambda Expression================
Lambda function body that takes that takes no parameter and returns void
name -->> Hari
Lambda function body that takes that takes String as a parameter and returns void
i -->> 6, j -->> 7
Lambda function body that takes that takes  two long as a parameter and returns double


Note: Like our facebook page to get updated of free classes and tutorials.