Java 8 Lambda Expression with Example

Definition and Benefits

Lambda expressions were introduced in Java. Java 8 Lambda Expressions are used to implement the Functional Interfaces. Functional Interfaces are those interfaces which has only one abstract method. Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface. Lambda expression is very important feature of Java which was included in Java 8. It provides a clear and compact way to represent one method interface using an expression. This is very helpful in java collection library.  Helps to iterate, filter and extract data from collection.

  • It saves a lot of code.
  • In case of lambda expression, we don’t need to define the method again for providing the implementation. Here, we just write the implementation code.
  • For Example  java.lang.Runnable, is a functional interface in Java 8 which has run() method as a abstract method.
Java 8 Lambda Expression Syntax

lambda parameter -> body

Java lambda expression is consists of three components.

1) lambda parameter: It can be empty or non-empty as well.

2) Arrow-token: It is used to link arguments-list and body of expression.

3) Body: It contains expressions and statements for lambda expression.

Zero parameter:
() -> System.out.println(“Zero parameter lambda”);

One parameter:–
(p) -> System.out.println(“One parameter: ” + p);
It is not mandatory to use parentheses.

Multiple parameters :
(p1, p2) -> System.out.println(“Multiple parameters: ” + p1 + “, ” + p2);

Example Without Lambda Expressions
interface Drawable
  public void draw(); 
public class LambdaExpressionExample { 
 public static void main(String[] args) { 
   int length=10; 

   //without lambda, Drawable implementation using anonymous class 
   Drawable d=new Drawable(){ 
     public void draw(){System.out.println("Drawing "+length);} 
Example With Java 8 Lambda Expressions
@FunctionalInterface //It is optional 
interface Drawable{ 
   public void draw(); 

public class LambdaExpressionExample2 { 
  public static void main(String[] args) { 
    int length=10; 

    //with lambda 
    Drawable d2=()->{ 
      System.out.println("Drawing "+length); 
Java 8 Lambda Expressions Example: Single Parameter
interface Sayable{ 
public String say(String name); 

public class LambdaExpressionExample{ 
   public static void main(String[] args) { 

 // Lambda expression with single parameter. 
   Sayable s1=(name)->{ 
     return "Hello, "+name; 

 // You can omit function parentheses 
  Sayable s2= name ->{ 
    return "Hello, "+name; 
Java 8 Lambda Expressions Example: Multiple Parameters
interface Addable{ 
int add(int a,int b); 

public class LambdaExpressionExample{ 
  public static void main(String[] args) { 

  // Multiple parameters in lambda expression 
   Addable ad1=(a,b)->(a+b);  

  // Multiple parameters with data type in lambda expression 
   Addable ad2=(int a,int b)->(a+b); 
Java 8 Lambda Expression Example: Creating Thread
public class LambdaExpressionExample{
 public static void main(String[] args) { 

 Runnable r1=new Runnable(){ 
   public void run(){ 
    System.out.println("Thread1 is running..."); 
Thread t1=new Thread(r1); 

Runnable r2=()->{ 
  System.out.println("Thread2 is running..."); 
 Thread t2=new Thread(r2); 
Java Lambda Expression with Collections

Sorting Collections with Comparator 

We can use Comparator interface to sort, It only contain one abstract method: – compare().

public int compare(Object obj1, Object obj2)
    compare() method working: –
  • returns negative value(-1), if and only if obj1 has to come before obj2.
  • returns positive value(+1), if and only if obj1 has to come after obj2.
  • returns zero(0), if and only if obj1 and obj2 are equal.

In List, Set, Map or anywhere else when we want to define our own sorting method, JVM will always call compare() method internally.

import java.util.*;

public class Demo { 
public static void main(String[] args) 
  ArrayList<Integer> al = new ArrayList<Integer>(); 
  System.out.println("Elements of the ArrayList " + 
  "before sorting : " + al);

 // using lambda expression in place of comparator object 
  Collections.sort(al, (o1, o2) -> (o1 > o2) ? -1 : (o1 < o2) ? 1 : 0);

  System.out.println("Elements of the ArrayList after" + " sorting : " + al); 


Elements of the ArrayList before sorting : [200, 101, 92, 245, 283]
Elements of the ArrayList after sorting : [283, 245, 200, 101, 92]

Important points:

  • The body of a lambda expression can contain zero, one or more statements.
  • Return type of anonymous function is same as the body of expression and if there is a single line of statement then curly brackets are not mandatory.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply