noscript

Java Lambda Expression – Complete Guide

In Java SE 8, Lambda expression was added as a new and important feature. An expression can be used to represent a single-method interface clearly and concisely. In a collection library, it is extremely useful as it assists in iterating, filtering, and extracting data from the collection.

A functional interface can be achieved by implementing an interface using the Lambda expression. It saves a lot of code. In cases of lambda expressions, we don’t need to define the method again to provide the implementation.

Functional Interface:

Lambda expression provides an implementation of functional interfaces. An interface that has only one abstract method is called a functional interface. Java provides an annotation @FunctionalInterface, which is used to declare an interface as a functional interface.

Use of Lambda Expression:

  1. To create a functional interface that is implemented.
  2. By writing very little coding.

Lambda Expression Syntax:

(argument_list) -> {body}

Three components make up Java lambda expressions.

  1. Argument_list: It has the option of either being empty or non-empty.
  2. Arrow token: It is used to link the arguments list and body of expression.
  3. Body: It contains statements and expressions for lambda expressions.

 

1 No Parameter Syntax:

 

() -> {

//Body of lambda expression

}

 

 

  1. One Parameter Syntax:

( b1 ) -> {

//Body of lambda expression

}

3 Two Parameter Syntax:

( b1, b2 ) -> {

//Body of lambda expression

}

Comparison with and without Lambda Expression :

 

Without Lambda Expression :

Ex:

interface Student{

public void info();

}

public class LambdaExpressionDemo {

public static void main(String[] args) {

int rollNo=10;

String name = “Nitin”;

//without lambda, Student implementation using anonymous class

Student d=new Student(){

public void info(){

System.out.println(“Student Information “+ rollNo         + ” ” + name);

}

};

d.info();

}

}

With Lambda Expression :

Ex:

interface Student{

public void info();

}

public class LambdaExpressionDemo {

public static void main(String[] args) {

int rollNo=10;

String name = “Nitin”;

//without lambda, Student implementation using anonymous class

Student d = () -> {

System.out.println(“Student Information “+ rollNo            + ” ” + name);

};

d.info();

}

}

Java Lambda Expression Example With No Parameter :

Ex:

interface SayHello{

public String welcome();

}

public class LambdaExpressionNoParameterDemo {

public static void main(String[] args) {

//Lambda expression with no parameter

SayHello s1 = () -> {

return “Welcome to IEC, Pune”;

};

System.out.println(s1.welcome);

}

}

 

Java Lambda Expression Example With One Parameter :

Ex:

interface SayHello{

public String welcome(String msg);

}

public class LambdaExpressionNoParameterDemo {

public static void main(String[] args) {

//Lambda expression with no parameter

SayHello s1 = (msg) -> {

return “Welcome to ” +  msg;

};

System.out.println(s1.welcome(“IEC, Pune”));

}

}

 

Java Lambda Expression Example With Multiple Parameters:

Ex:

interface SayHello{

public String welcome(String msg1, String msg2);

}

public class LambdaExpressionNoParameterDemo {

public static void main(String[] args) {

//Lambda expression with no parameter

SayHello s1 = (msg1, msg2) -> {

return “Welcome to ” +  msg1 + “ and we are providing ” +           msg2;

};

System.out.println(s1.welcome(“IEC, Pune”, “IT courses.”));

}

}

                                                 

 

                                                  For more information & classes Call: 2048553004
                                                  Registration Link: Click Here!

Author: Ambarish Durani

JAVA Trainer

IT Education Centre Placement & Training Institute

© Copyright 2023 | IT Education Centre.