Java 8 Lambda Expression

By Dhiraj Ray, 12 May,2017  

This article is all about java 8 lambda expressions. Here we will discuss about what is lambda expression, why is it required although we have object oriented programming, different ways of using lambda. We will also write a sample hello world java program using lambda expression and functional interface and many more. So lets get started.

What is Lambda Expression

Lambda is a programming paradigam that enables functional programming. Lambda expressions were introduced as a part of Java 8 and ever since it has been introduced it has simplified the development process to a greater extent. As we know in the object oriented programming model, a method is associated to a class but in case of functional programming a method can be created without belonging to any class and can be reused anywhere without creating any instance of an object and can be executed on demand. Hence, it is also said that lamda expressions are lazy loading.

Lambda expression represents inline function which does not belong to a class and these functions are treated as a value in functional programming and this introduced the concept of Functional Interface in java 8.

Functional Interface

Functional interface is an interface that performs only a single operation as we can only declare single abstract method in a functional interface.

Why Lambda Expression

Lambda enables functional programming and its not like functional programming does something which is not possible through object oriented programming. The same implementations of object oriented programming can be achieved in functional progamming in a concise manner with more clearer and flexible codes.We can avoid many boiler plates code with functional programming. It also enables parallel processing.

Lambda Expression Syntax

Following is the syntax of a Lambda expression.

(parameters) -> {statements;}

The parameters can be either empty or can have multiples.Following is the example.

Zero Parameter
() -> System.out.println("Zero parameter");
One Parameter
(p) -> System.out.println("Single parameter" + p);
Multiple Parameters
(p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2);

The statements are the body of a function.If you have a single statement you can write a lambda expression as:

() -> System.out.println("Single line statements.");

Similarly, if you have multiple lines of statements, your lambda expression looks something like

(p1, p2) -> { System.out.println("parameter1" + p1); System.out.println("parameter2 " + p2); }

Hello World Example with Lambda Expression

Following is a method that prints Hello World in object oriented programming.

public void hello(){ System.out.println("Hello World"); }

Above code using lambda expressions will be something like

() -> System.out.println("Hello World");

Returning Value From Lambda Expression

Following is a lambda expression syntax that accepts two integers and returns sum of it.

(int a, int b) -> return a+b;
Note:If the type of the parameters can be decided by the compiler, then we do not require to declare type.

(a,b) -> return a+b;

If the body has just one expression, the return keyword can also be omitted.

(int a, int b) -> a + b

Executing Lambda Expression

Let us create a functional interface first. Functional interface is an interface that has only one abstract method.

package com.devglan; public interface FunctionalInterface { void hello(); }

Now we can execute it as follow. Lambda expression is assigned to a variable first, and then the lambda expression is invoked by invoking the interface method it implements.

package com.devglan; public class FunctionalInterfaceExample { public static void main(String[] args) { FunctionalInterface lambda = () -> {System.out.println("Hello World");}; lambda.hello(); } }

Conclusion

I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.

References

Oracle Docs

Lambda Architect

Lambda Expressin wiki

Suggest more topics in suggestion section or write your own article and share with your colleagues.

Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.

Further Reading: