What Are Lambda Expressions in Java And How Can We Use Them?

1.What Are Lambda Expressions?

A lambda expression is, essentially, an anonymous (that is, unnamed) method. However, this method is not executed on its own. Instead, it is used to implement a method defined by a functional interface. Thus, a lambda expression results in a form of anonymous class. Lambda expressions are also commonly referred to as closures.

what the above definition defines can be ellobarated with an example

@FunctionalInterface  //It is optional  
interface Clickable{
public void click();
}

public class LambdaExpression {
public static void main(String[] args) {
int force=10;
//Without Lambda
Clickable d1=new Clickable() {
@Override
public void click() {
System.out.println("Without lambda Exp");
}
};
d1.click();


//with lambda
Clickable d2=()->{
System.out.println("Cliking "+force);
};
d2.click();
}
}

As you can see that when the class implements a method from the functional interface we have to create an object of the interface and then write the actions and call it by calling the method via object

But when we use the lambda expression the whole method could be assigned to the variable as if the method can be accessed with just the help of the variable name.

2.How can we create a Lambda Expression?

 The new operator, sometimes referred to as the lambda operator or the arrow operator, is −>. It divides a lambda expression into two parts. The left side specifies any parameters required by the lambda expression. (If no parameters are needed, an empty parameter list is used.) On the right side is the lambda body, which specifies the actions of the lambda expression. The −> can be verbalized as “becomes” or “goes to.”

Example 1: Java Lambda Expression with no parameter

@FunctionalInterface
interface MyFunctionalInterface {

	//A method with no parameter
    public String sayHello();
}
public class Example {

   public static void main(String args[]) {
        // lambda expression
    	MyFunctionalInterface msg = () -> {
    		return "Hello";
    	};
        System.out.println(msg.sayHello());
    }
}

Output:

Hello

Example 2: Java Lambda Expression with single parameter

@FunctionalInterface
interface MyFunctionalInterface {

	//A method with single parameter
    public int incrementByFive(int a);
}
public class Example {

   public static void main(String args[]) {
        // lambda expression with single parameter num
    	MyFunctionalInterface f = (num) -> num+5;
        System.out.println(f.incrementByFive(22));
    }
}

Output:

27

Example 3: Java Lambda Expression with Multiple Parameters

interface StringConcat {

    public String sconcat(String a, String b);
}
public class Example {

   public static void main(String args[]) {
        // lambda expression with multiple arguments
    	StringConcat s = (str1, str2) -> str1 + str2;
        System.out.println("Result: "+s.sconcat("Hello ", "World"));
    }
}

Output:

Result: Hello World

Example 4: Iterating collections using foreach loop

import java.util.*;  
public class Example{  
    public static void main(String[] args) {       
       List<String> list=new ArrayList<String>();  
       list.add("Rick");         
       list.add("Negan");       
       list.add("Daryl");         
       list.add("Glenn");         
       list.add("Carl");                
       list.forEach(          
           // lambda expression        
           (names)->System.out.println(names)         
       );     
    }  
}

Let us know whether you understood the above concept.

Source:Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects) (oracle.com)

Happy Coding Mates:)

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Shopping Cart