Hello everyone,
Welcome to the Java Post series of CodeCafeX. Are you tired of writing lengthy codes for your project? Don't be afraid. Java Lambda expressions are the solution for this. Today, I'll be discussing Java Lambda Expressions.
Java Lambda Expressions was introduced with Java 8. We can use Lambda expressions to reduce our code length and complexity.
Java lambda expressions are just like a method. This can return values, and also we can pass parameters to this. The main difference between a normal method and a Java lambda expression is that the Java lambda expression does not need any specific name.
We can define lambda expressions in Java like this. This is the basic syntax for Lambda expressions in Java.
(parameter list) -> lambda body
The new operator ( -> )
is known as an arrow operator or a lambda operator.
Think, we have a method like this.
public double getValueOfPi() {
return 3.141592;
}
We can write a lambda expression for the above method like this.
() -> 3.141592
This method has no parameters because the operator's left side includes an empty parameter. The right side is the lambda body specifying the action of the lambda expression, which returns the value 3.1415.
There are two types of lambda bodies.
() -> System.out.println("The body with a single expression");
This is called the expression body for a lambda.
() -> {
double sumOfTwoNumbers = 123 + 456;
return sumOfTwoNumbers;
}
This type of body is called a block body. This body allows us to implement multiple code statements inside that body.
Example 1.
Using a lambda expression, let's write a Java program to return the sum of two values. A lambda expression implements an abstract
method defined by a functional interface, so we need to define a functional interface first. We can use @FunctionalInterface
annotation to implement the functional interface. The main thing is that we can use only one abstract method in a functional interface.
@FunctionalInterface
public interface MyTestInterface {
double getSumOfTwoNumbers();
}
public class Main {
public static void main(String[] args) {
// declare a reference to MyTestInterface
MyTestInterface ref;
// lambda expression
ref = () -> 123 + 456;
System.out.println("Value of Two numbers = " + ref.getSumOfTwoNumbers());
}
}
Here, we have created the functional interface called MyTestInterface. This interface contains only a single abstract method.
That method is named as getSumOfTwoNumbers()
.
Inside the main Class, we have declared the reference of MyTestInterface. We cannot instantiate the interface. Because MyTestInterface is an abstract.
Then, we assigned the lambda expression to the interface.
ref = () -> 123 + 456;
Finally, we called the getSumOfTwoNumbers()
method using the reference.
Till now, we talked about the lambda expressions without any parameters. Let’s talk about lambda expressions with parameters.
For example
(i)-> (i%2)==0
The variable i inside the parenthesis is a parameter passed to the lambda expression. The lambda body takes the parameter and checks if the value is even or odd.
Example 2.
@FunctionalInterface
public interface MyTestInterface {
double getSumOfTwoNumbers(int num1,int num2);
}
public class Main {
public static void main(String[] args) {
// declare a reference to MyTestInterface
MyTestInterface ref ;
// lambda expression
ref = (num1, num2) -> (num1+num2);
System.out.println("Value of Two numbers = " + ref.getSumOfTwoNumbers(123,456));
}
}
Here, we have created the functional interface called MyTestInterface. This interface contains only a single abstract method.
That method is named as getSumOfTwoNumbers(int num1,int num2)
.
Inside the main Class, we have declared the reference of MyTestInterface.
Then, we assigned the lambda expression to the interface.
ref = (num1, num2) -> (num1+num2);
Finally, we called the getSumOfTwoNumbers(int num1,int num2)
method using the reference.
Avoid excessive use of default methods in functional interfaces. Adding too many unnecessary default methods into a functional interface would be a poor architectural decision.
Use standard functional interfaces from the Java .util.Function package for utilizing Lambda Expressions in Java. This will be beneficial for developers in practical scenarios.
Use the @FunctionalInterface annotation habitually. This special annotation is exclusively for functional interfaces, making it easier to identify them in complex code and avoid confusion with other interface segments.
Now you have a better idea about the meaning of the Java lambda expression, how to use it, and the syntax of the lambda expression. If you have any questions, don't hesitate to email me. In the next Tutorial, I will discuss Java stream API.