Java 8 Feature – Lambda Expressions
Lambda Expressions feature is available in Java from Java 8 onwards. This feature is one of the biggest and great addition in Java 8 as it encourages functional programming. It is extremely useful for Java developers as using this developers can write very concise and clean code.
Functional Interface’s method is implemented beautifully using Lambda Expression.
Lambda Expressions enables developers to treat –
Functionality as method argument
OR
Code as Data
Syntax
- ZERO parameter – Syntax for Lambda expression without any parameter is given below –
() -> expression |
- ONE parameter – Syntax for Lambda expression with ONE parameter is given below –
parameter -> expression OR (parameter) -> expression |
- More than ONE parameter – Syntax for Lambda expression with more than one parameters is given below –
(parameter1, parameter2) -> expression OR (parameter1, parameter2) -> {code block} |
Java Code Examples of Lambda Expressions
ZERO Parameter
Java Code Example of Lambda expression without any parameter is given below –
Java Code
package com.thinkconstructive;
@FunctionalInterface
interface Welcome {
public void welcomeMessage();
}
public class NoParam {
public static void main(String args[])
{
// With Lambda Expression
Welcome welcome = () -> System.out.println("Welcome to Think Constructive");
welcome.welcomeMessage();
}
}
Output
Welcome to Think Constructive
Explanation
Above example, demonstrates the usage of Lambda Expressions with no parameter scenario. Welcome is a Functional Interface i.e. it has exactly one Abstract Method called welcomeMessage() . This method is implemented and called in main() method using Lambda Expression with just one line of code.
ONE Parameter
Java Code Example of Lambda expression with exactly ONE parameter is given below –
Java Code
package com.thinkconstructive;
@FunctionalInterface
interface ReturnValExample {
public String welcomeMessage(String message);
}
public class OneParamWitnReturnValue {
public static void main(String args[])
{
// Without Lambda
ReturnValExample returnValExample = (message) -> {
return message+"Think Constructive";
};
System.out.println( returnValExample.welcomeMessage("Welcome to "));
}
}
Output
Welcome to Think Constructive
Explanation
Above example shows the usage of Lambda Expressions code with ONE parameter. There is a Functional Interface named as ReturnValExample which has an Abstract Method called welcomeMessage(message) . This method accepts one argument and returns a String value. welcomeMessage() method is implemented and called with just one liner code using Lambda expressions in main() method.
More than ONE Parameter
Java Code Example of Lambda expression with MORE than ONE parameter is given below –
Java Code
package com.thinkconstructive;
@FunctionalInterface
interface WelcomeWithTwoParam {
public void welcomeMessage(String messageOne, String messageTwo);
}
public class TwoParam {
public static void main(String args[])
{
//Without Lambda
WelcomeWithTwoParam welcomeWithTwoParam = (messageOne, messageTwo) -> {
System.out.println("Welcome to " + messageOne + " " + messageTwo);
System.out.println("This is Lambda Expression");
};
welcomeWithTwoParam.welcomeMessage("Think", "Constructive");
}
}
Output
Welcome to Think Constructive
This is Lambda Expression
Explanation
Above example demonstrates the code for Lambda Expressions with 2 parameters. WelcomeWithTwoParam is a Functional Interface with exactly ONE Abstract Method named as welcomeMessage(). This method takes 2 parameters arguments. welcomeMessage() is called and implemented by main() with only one line of code using Lambda Expression.
CODE Repository
Demo Code of Lambda expressions can be found at GITHUB
Video Tutorial
Video explanation of Lambda Expression is HERE