Lambda Expression | Java

/Lambda Expression | Java

Introduction

Lambda expression was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. The Lambda expression is used to provide the implementation of an interface which has functional interface. It saves a lot of code. Java lambda expression is treated as a function, so compiler does not create .class file.

 

Functional Interface

Lambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface. Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface. We can use lambda expressions only with Functional interfaces.

// Functional Interface example
public interface FunctionalInterface
{
  public void show();
}

 

Method Reference

A method reference is the shorthand syntax for a lambda expression that executes just ONE method.

// General syntax of a method reference
Object :: methodName

 

Since lambda expressions can be used instead of using an anonymous class.

// Not using method reference
Consumer<String> c = s -> System.out.println(s);

// Using method reference
Consumer<String> c = System.out::println;

 

Syntax

(argument-list) -> {body}

Lambda expression is consisted of three components.

  1. Argument-list: 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.

 

Important Points

  • A lambda expression can have zero, one or more parameters.
  • The type of the parameters can be explicitly declared or it can be inferred from the context.
  • Multiple parameters are enclosed in mandatory parentheses and separated by commas. Empty parentheses are used to represent an empty set of parameters.
  • Body of the lambda expressions can contain zero, one or more statements. If body of lambda expression has single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression. When there is more than one statement in body than these must be enclosed in curly brackets.

Example

// Classic Comparator
Comparator<Developer> byName = new Comparator<Developer>() {
  @Override
  public int compare(Developer o1, Developer o2) {
    return o1.getName().compareTo(o2.getName());
  }
};

// Lambda expression equivalent
Comparator<Developer> byName = 
  (Developer o1, Developer o2)->o1.getName().compareTo(o2.getName());

 

Example using method reference

// Function interface
interface Sayable{  
  void say();  
}  

// Public class
public class MethodReference {  

  public static void saySomething(){  
      System.out.println("Hello, this is static method.");  
  }  

  public static void main(String[] args) {  
    // Referring static method  
    Sayable sayable = MethodReference::saySomething;  
    
    // Calling interface method  
    sayable.say();  
  }  
}

 

 

July 24th, 2019|Categories: Programming|Tags: |
avatar
  Subscribe  
Notify of