Lambda Expression in Java 8

Reading Time: 3 minutes

In this blog we will understand what is the lambda expression and why we need lambda expression and how we use lambda and about the functional interface.

What is Lambda Expression :

  • It is an anonymous function.
  • Not having name
  • No return type and no modifiers.

Syntax of Lambda :

The syntax of a lambda consists of the following:

  1. A comma-separated list of formal parameters enclosed in parentheses. In this case, it is (Cat m, Cat n)
  2. The arrow token ->
  3. A body, which consists of a single expression or a statement block. In this case, it is one single expression – Integer.compare(m.getHeight(), n.getHeight())
lambda operator (->) 

Example

() -> System.out.println("Example of lambda expression!!");

Normal java method which prints two value sum –

public void sum(int a, int b){
System.out.println(a+b);
}

Now try to convert this method in lambda –

(int a, int b) -> {
System.out.println(a+b);
}
  • IF your code only one line you can remove the curly braces.
(int a, int b) -> System.out.println(a+b);

Example –

public int square(int n){
return n*n;
}

Now try to convert this method –

(int n) -> return n*n;
n -> n*n;

Sorting with Java 8 Lambdas Expression

In Java 8, we can do sorting in one simple line of code like this:

Arrays.sort(catArray, (Cat m, Cat n) -> m.getHeight() - n.getHeight());
printCats(catArray);

(Cat m, Cat n) -> Integer.compare(m.getHeight(), n.getHeight()) is a lambda . It is converted to an object of Comparator behind the scene. For now let’s simply consider the lambda as a function. How lambda expressions are converted to objects of functional interfaces is a more complicated story.

What benefits are there If we used Lambda in java?

  • Less lines of codes.
  • Higher Utilising Multi core CPU’s
  • Enable functional programming in java.

Where we can use ?

It use anywhere in java 8 where we want less line of codes before java 8 if we do anything we write lots of codes, let suppose we sort an array before java 8 we write 10-12 line of code do the sorting, but after java 8 we do this in 1-2 lines the java 8 less line of codes.

What is Functional interface?

Those interfaces which contains single abstract method and n number of non-abstract method is called function interface. we define an annotation @FunctionalInterface to define the functional interface but it not mandatory but we define it then compiler understands this is the functional interface and it contains only one abstract method and if defines multiple abstract methods then the compile gives the error.

Example

@FunctionalInterface
interface interf1 
{
public abstract void m1();
default void m2()
{
 // some code here 		
}
static void m3()
{
 // some code here 	
}
}

Example of functional interface :

  • Runnable
  • ActionListener
  • Comparable

Conclusion :

  • After the lambda in java, the number of line code is less and without lambda can’t possible enable the functional programming.
  • lambda is made functional programming possible in java without it can’t think about the enable functional programming in java.
  • If we understand deeply about functional programming, it is nothing but a way to define your code to a method in java.
  • Lambda allows passing a custom code to the java method which can not be possible before java 8 and also you write the code without the parameter. If your code has only one line then parameters are not compulsory then you don’t require the parameter.
  • If your code has two or more lines then the curly brace is required and the same in the return keyword is mandatory when you have two or more lines but if you have one line then the return statement is optional.

References :

For more information on Lambda Expression, Click here.

Written by 

Sumit Raj is a java developer having experience of more than 2.7+ years. He is very interested to solve complex things in programming and also learn new technologies.