Java

Lambda Expressions in Java | Explained

Lambda expressions are an exciting addition to Java SE 8. Lambda expressions assist us in building java applications in a different paradigm as compared to traditional OOP. Lambda expressions are a fundamental approach to Java functional programming and hence allow a function to be created without belonging to any specific class. The concept of lambda expressions revolves around the functional interface.

This write-up presents a detailed understanding of the below-listed aspects of java Lambda expressions:

  • What are Lambda Expressions
  • Why Lambda Expressions
  • What is a Functional Interface in Java
  • Lambda Expression Parameters
  • How to Implement Lambda Expressions in Java

So, let’s start!

What are Lambda Expressions

In java, lambda expressions are nothing but a concise block of code that can be used to write anonymous methods. The lambda expressions are very similar to the java methods, but these expressions do not need a name and can be implemented within the function’s body.

Why Lambda Expressions

There are numerous advantages/features that show the significance of lambda expressions in java:

  • It enables functional programming.
  • Lambda expressions make the code easily concise and easily readable.
  • Lambda expressions assist us in writing easy-to-use APIs and libraries.
  • Lambda expressions support parallel processing.

Functional Interfaces

In Java, an interface can have at least one abstract method; however, if we talk about a functional interface, It has exactly one abstract method. For example, the Java Runnable interface is a functional interface that comprises only one abstract method i.e. run(). Lambda expressions implement only abstract methods and hence lambda expressions can implement the functional interfaces.

Example

Let’s have look at the below code snippet where you can identify what is a functional interface in java:

interface PrintData{
  void show(String data);
}
public class LambdaExpressionExample implements PrintData {
 
    public void show(String data) {
        System.out.println(data);
    }
 
    public static void main(String[] args) {
        LambdaExpressionExample obj = new LambdaExpressionExample();
        obj.show("Example of Lambda Expression");
    }
}

The output of the above code is shown in the following snippet:

This is how a functional interface works in java.

Lambda Expression Parameters

In Java, the arrow -> operator was introduced via lambda expressions. The arrow operator is responsible for dividing the lambda expression into two parts i.e. parameters/arguments, bode.

A lambda expression can have no parameter, single parameter, or multiple parameters the basic syntax for both approaches is shown below:

Syntax of Lambda Expression with zero Parameter

() -> {
//code
}

Syntax of Lambda Expression with single Parameter

Parameter -> {
//code
}

Syntax of Lambda Expression with Multiple Parameters

(parameter1, parameter2, parameter3) -> {
//code
}

How to Implement Lambda Expressions in Java

The below-given code snippet will let you understand how to implement the lambda expressions in java:

Example

In this example we will pass the Lambda expression as parameters to the LinkedList’s forEach() function to print its each element:

public class LambdaExpressionsExample {
    public static void main(String[] args) {
        LinkedList subjects = new LinkedList();
        subjects.add("Computer Science");
        subjects.add("Mathematics");
        subjects.add("Biology");
        subjects.add("Physics");
        subjects.add("English");
        subjects.forEach((sub) -> {
            System.out.println(sub);
       });
    }
}

The complete code snippet and respective output is shown in the below-given snippet:

The output authenticates the working of the lambda expression as it succeeds in printing every item of the list.

Example

The below-given code snippet elaborates the working of the java lambda expression with multiple parameters/arguments

interface FindProduct {
    int product(int x, int y);
}
public class LambdaExpressionExample {
    public static void main(String[] args) {
        FindProduct num = (x, y) -> (x * y);
        System.out.println(num.product(12, 7));
}
}

The above snippet calculates the product of two numbers using the java lambda expression:

The above program calculates the product of two numbers successfully which authenticates the working of lambda expressions in java.

Conclusion

Lambda expression in java is an anonymous method that neither has a name nor belongs to any class and it provides a clear and concise way of representing a method interface through an expression. The lambda expressions provide the implementation of a functional interface and simplify the software development. This write-up provides a detailed overview of java lambda expressions where we have learned what a lambda expression is, why it is important, and how to implement lambda expressions in java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.