Lambda expressions are similar to methods and are a short block of code that takes in parameters and returns a value. They are added in Java 8. In this article, we will learn about lambda expressions in detail covering all the What, Why, Where, When, When Not! Just to have an idea first:
But first, let's learn about the pre-requisite before deep diving into the lambda expressions.
Lambda expressions are nothing but the implementation of the functional interface. A functional interface is nothing but an interface that has only one abstract method. To declare an interface as a functional interface, Java provides an annotation @FunctionalInterface.
What are Lambda Expressions?
They are anonymous or unnamed method that does not execute on their own. They are like functions that take arguments and do some functionality and return the corresponding value. Lambda expressions don't need a name and can be implemented in the body of the method itself.
Lambda expressions are defined using the below declaration:
Let's take an example of how we use a lambda expression.
Consider the below method which returns us a pi value:
Using lambda expression, we can write this method as:
In the above example, the left side does not have any parameters, and then we have the ( -> ) arrow operator and then on the right side, we have the functionality.
Note that :
- lambda expressions are used to implement a method defined by a functional interface.
- It is the one way to represent one method interface using an expression and helps to iterate, filter, and extract data from the collection (An example is java.lang.Runnable). They are treated as a function, so the compiler does not create a .class file.
Let's take one more example :
Now we will write a program to return the value of pi, as we did above using the lambda expressions. We know that lambda expressions do not execute on their own. Instead, they form the implementation of the abstract method defined by the functional interface.
So, we have to first define a functional interface:
In the above example,
- We first created a functional Interface named "MyInterface" which has one abstract method named "getPiValue()".
- Then in the main class, a reference to MyInterface has been declared (Note: We can declare the reference to an interface but we cannot instantiate an interface).
- Then we store the lambda expression having pi value to reference named "ref".
- Finally, we call the getPiValue() method to call the lambda expression.
Why Use Lambda Expressions?
- It can be created as a function without belonging to any class.
- To illustrates the implementation of the Functional interface.
- Reduced code length.
Advantages of Lambda Expressions
There are many advantages of using lambda expressions as given below:
Reduction in code bloat
Elimination of shadow variables
Encouragement of functional programming
Enhanced iterative syntax
Simplified variable scope
Less boilerplate code
JAR file size reductions
Parallel processing opportunities
Java Lambda Expression Syntax
It consists of only a parameter list and an expression. In simpler terms, it consists of the following:
- Argument-list: It can contain any number of arguments/parameters. For example 0, 1, 2, and so on...
- Arrow-token: Used to link an arguments-list and an expression.
- Body: Contains expressions and statements for lambda expression.
Note: The way functions accept parameters similarly lambda expressions accept parameters.
Now, let us look at syntax for lambda expression:
Syntax: without any parameter (No Parameter)
Syntax: with 1 parameter
Syntax: with 2 parameters
Java Program Without Lambda Expression
Shown below is the java program in which we are not implementing any Java lambda expression but we are implementing an interface without using a lambda expression:
Now, let's take a look at various examples implemented using lambda expressions given below.
Java Programs With Lambda Expression
01) foreach() method:
In the below, we have used ArrayList's foreach() method to print every item in the list:
02) No parameter:
03) One parameter:
04) Two parameters:
05) Creating Thread:
06) Event Listener:
Lambda expression's body can contain zero, one, or more statements. In the case of a single statement, curly brackets are not required and the return type of both the anonymous function and the body expression will be the same.
In this article, we looked at the lambda expression's definition, declaration, and syntax with multiple examples. Congratulations on getting this far! Now give yourself a pat on the back. Good job!