Functional Interfaces & Lambda Expressions in Java
Lambda expressions are an important feature introduced in Java 8. They provide a clear and concise way to represent a method interface using an expression. Lambda expressions are similar to methods in java wherein they take a parameter and return a value, however, the key difference between a method and lambda expression is that they do not mandatorily need a name.
What are the advantages of using Lambda Expressions?
Lambda Expressions converts the code segment into an arguement.
The useage of these expressions is independent of class instantiation.
These expressions can be treated as objects.
How to define Lambda Expressions in Java?
Lambda Expressions are essentially unnamed methods, however, these methods do not execute on their own instead it is used to implement a method defined by a functional interface.
An interface that has only one abstract method is called a functional interface in Java. The Runnable interface from the java.lang package is an example of a functional interface as it contains only one method run(). Java also provides an annotation @FunctionalInterface to denote that the interface is functional.
The Lambda Expressions are denoted with the help of a lambda operator also known as an arrow operator ( -> )
Consider the following example to understand the lambda expressions.
The below code snippet denotes a method getPiValue() which return the value of Pi as 3.1415
The same can be represented in a single line using the Lambda Expression as shown below
In Java, the Lambda Expressions body are of two types:
- A body with a single expression : In this type of expression, the body consists of a single statement, as shown in the below example.
2. A body with a block of code : A block body allows lambda expressions to have multiple lines of code and are enclosed in braces followed by a semicolon at the end of the block.
Example of using Lambda Expressions to display a message:
In the above example, we have a functional interface, MessageDisplay that has only one method show(). Inside the main class, we have assigned a reference to the MessageDisplay interface and then assigned a lambda expression to the reference, and then we call the show() method using the reference value.
One of the most important uses of Lambda Expression is with the Stream API. The Stream API has been added to the java.util.stream package in Java 8 which allows the developers to perform a wide variety of operations like search, filter, map, reduce or manipulate collections like Lists.
Consider an example where our input data is a list of strings formed by combining the name of a country and the name of a city in that country. Our task is to process this data and return the name of places in Nepal
Here we are using the methods: filter(), map(), and forEach() from the Stream API that can take a lambda expression as input.
One of the major advantages of Lambda Expressions is that they reduce the length of the code drastically and make the code more readable once understood clearly.
They also support sequential and parallel processing in Java by passing behavior as an argument in the methods.
By using Stream API and Lambda Expressions, we can achieve higher efficiency (parallel execution) in case of bulk operations on collections. Also, lambda expression helps in achieving the internal iteration of collections rather than external iteration. Considering all the advantages offered by Lambda Expressions they have proved to be quite useful in real-time software development.