Skip to main content

Lambda expression in java 8

Lambda expression in java 8

Before starting discussion about lambda expression in java 8. First of all need to understand lambda expression.
In programming language world, lambda expression is a anonymous function.
LISP is the first programming language which uses lambda expression. 
Given below programming languages are already started using lambda expression-

  • Python
  • C
  • C++
  • Objective C
  • RUBY
  • C# .NET
  • SCALA

Finally java also started using lambda expression.


Benefits of using lambda expression in java 8

  • Get functional programming benefit in java.
  • Write more readable, maintainable, clean and concise code.
  • Use API very easily and effectively.
  • Enable parallel processing in java.

java lambda expression syntax

Rupendra Sharmaadmin

(argument-list) -> {body}

Java lambda expression is divided into 3 sections.
i) Argument-list: It holds arguments.
ii) Arrow-token: Link arguments-list and body of lambda expression.
iii) Body: Statements for lambda expression goes here.


java lambda expression example : With no parameter

Given below program prints welcome message by using lambda expression.

@FunctionalInterface    // Optional
public interface Showable {
	public void show();
}

public class TestLambdaExp {
	public static void main(String[] args) {
		Showable showable = () -> System.out.println("Welcome in java 8 world.");
		showable.show();
	}
}

Output:

Welcome in java 8 world.
Note
1- In lambda expression modifier and return type concepts are not applicable.
2- In given above program only given below block of code is lambda expression-
( ) -> System.out.println(“Welcome in java 8 world.”);
3- Functional interface is only use for invoke lambda expression. In next article we will discuss in more detail about functional interface.

java lambda expression example : With single parameter

@FunctionalInterface
public interface Showable {
	public String show(String name);
}

public class TestLambdaExp {
	public static void main(String[] args) {
		// Single parameter without specify data type
		Showable showable1 = (name) -> "Hello, " + name;
		System.out.println(showable1.show("Deepak"));

		// Remove parentheses
		Showable showable2 = name -> "Hello, " + name;
		System.out.println(showable2.show("Deepak"));

		// Single parameter with data type
		Showable showable3 = (String name) -> "Hello, " + name;
		System.out.println(showable3.show("Deepak"));
	}
}

Output:

Hello, Deepak
Hello, Deepak
Hello, Deepak

java lambda expression example : With multiple parameters

public interface ArithmeticOpeation {
	public int sum(int a, int b);
}

public class TestLambdaExp {
	public static void main(String[] args) {
		ArithmeticOpeation result1 = (int a, int b) -> (a + b);
		System.out.println("Result 1 : " + result1.sum(10, 10));

		ArithmeticOpeation result2 = (a, b) -> (a + b);
		System.out.println("Result 2 : " + result2.sum(20, 20));

		ArithmeticOpeation result3 = (a, b) -> {
			return (a + b);
		};
		System.out.println("Result 3 : " + result3.sum(30, 30));
	}
}

Output:

Result 1 : 20
Result 2 : 40
Result 3 : 60

Leave a Reply

Your email address will not be published. Required fields are marked *

Top