Java

Java Lambda Expression with example

Java Lambda Expression with example

Lambda expression is a feature introduced in Java 8. Lambda expression replaces anonymous function that does’t have a name and any class. It replaces a need for single class or anonymous inner class to write a piece of code. It makes our code more clean and readable by removing all boilerplate code. Lambda expression in java has below main parts.

  1. Name: It doesn’t have name as it is anonymous.
  2. Parameters: It accepts parameters same like normal methods.
  3. Body : Body contains the main logic of the function.
  4. Return Type: No need of explicit return type as Java 8 compiler is intelligent enough to infer the return type based on code.

Java Lambda Expression Syntax:

Below is the syntax for creating a lambda expression. We specify input parameters if any on left side of the operator -> and place the block of statement or expression on right side of it.

 

 

above expression specifies a lambda expression that takes two parameters x and y and returns its sum.

Benefits of Lambda Expression in Java:

  • Conciseness
  • Reduction in boilerplate code
  • Improves readability and code reuse
  • Functional programming

When and Where to use Java Lambda Expression:

Lambda expressions in java is used to replace anonymous method definition or anonymous inner classes. To use lambda expression, we need functional interface (Functional interface is an interface with only single abstract method). Functional interface can be created using annotation @FunctionalInterface otherwise we can use functional interface provided by java. Look at the below example to create a thread in java.

Prior to Java 8: Without using lambda expression we were using anonymous inner classes to implement single abstract method in Runnable interface for creating Thread.

After Java 8: By using lambda expression instead of creating anonymous class, we can write method definition directly in single line.

From above example we can see that lambda expression reduces the lines of code drastically, and the code is much more readable and concise. Lambda expression is backward compatible.

Custom Functional Interface:

Lets have a look at how to implement our custom functional interface. First we need to create an interface and mark that interface with annotation with @FunctionalInterface

Our functional interface SquareRoot has only one abstract method with single parameter to compute the square root. Below is the implementation of functional interface by lambda expression.

In above lambda expression you can see Line 1 has the implementation of the functional interface in one line. Also if you notice if we have only 1 parameter than in that case ( ) parentheses are not required on left for parameter,  also curly braces { } are not required on right if it has single statement.

Line 2 actually calls the lambda expression that we have implemented in Line 1 to get the result.

Lambda Parameters:

Zero parameter:

If the method you are matching your lambda expression with takes no parameter, then you can write it like below.

 

One Parameter:

If the method you are matching your lambda expression with takes exactly one parameter then you can write your lambda expression like this.

 

When it takes one parameter it can also be written like this, where parentheses can be omitted.

 

Multiple Parameters:

If the method takes more than 1 parameters then the parameters need to be listed inside the parentheses

 

Custom Type as Parameter:

If we have some custom object that we need to pass in the method as parameter, in those cases compiler will not infer the type and it will tell us to supply one. To supply the type of parameter in those cases we just need to add it before the variable name just like we declare elsewhere. Here is the syntax how it looks.

 

Read more about Java 8 Features-

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.