Lambda Expressions in Java With Examples

Java Lambda expression is one of the most important addition in Java 8. It is a step towards functional programming in Java programming language. This Java Lambda expression tutorial gives an overview of lambda expressions like what are lambda expressions, how to write one and what are the advantages of using lambda expressions.

Functional interface in Java

One thing you should know about is the functional interface before delving deep into lambda expressions.
A functional interface is an interface with only a single abstract method (SAM). There are already many functional interfaces in Java  (though many new ones added in Java 8) like Runnable with its single run() method, Callable with its call() method or Comparator with its compare() method.

To know more about Functional interfaces in Java please refer this post- Functional Interface in Java

Lambda expression in Java

Java lambda expression is an implementation of a functional interface. It is an instance of a functional interface and the lambda expression you write implements the abstract method of the functional interface.

Let’s try to clarify it with an example. Generally when you implement a functional interface most probably you will implement it as an anonymous class. For example if you have to implement a Comparator you will write it as an anonymous class as given below-

Since Comparator is a functional interface it can also be implemented as a Lambda expression.

Some important points you should notice here are-

  1. By implementing a functional interface as a lambda expression, code is more compact.
  2. If you see the Collections.sort() method what you have done is written a function (Integer o1, Integer o2)->o2.compareTo(o1) that represents an instance of a functional interface and it can be passed as a method argument. That is one of the advantages of lambda expression you can pass functionality as an argument to another method.

Arrow operator or Lambda operator

Java lambda expressions introduce a new operator -> knows as Arrow operator in Java. Arrow operator has two parts-

Left side specifies parameters required by the lambda expression which can be empty too if there are no parameters.
Right side is the lambda body which is actually the code of your lambda expression.

So the syntax of Java Lambda expression is as follows-

Type can be inferred from the context in which lambda expression is used so you don’t need to explicitly specify type of the parameters.

If we consider the lambda expression we have already written-
Lambda expression in Java

Lambda expressions in Java examples

Now when you have a good idea of functional interface and how to write a lambda expression let’s see some more examples of Lambda expressions in Java.

1- The simplest lambda expression is one with no parameters and just a return value and it can be written as.

Here is a class where this lambda expression is put to use.

Output

Some important points you should notice here are-

  • Since lambda expression is an instance of functional interface so it can be referred using a functional interface reference as done in this statement- TestInterface ref = () -> 10;
  • Since there are no parameters so left side of the arrow operator is just empty parenthesis.

2- A lambda expression with parameters.

Output

Here we have a lambda expression with two String parameters. You don’t need to specify types of the parameters as that can be inferred from the context it is used, so this is also acceptable-

Lambda expression as a method argument

In the beginning we have already seen an example where comparator implementation as a lambda expression is passed as a method argument. This is one of the feature of the lambda expression; to treat functionality as method argument.

If you have a method where argument is a functional interface then you can pass a lambda expression compatible with that functional interface as a method parameter.

In the following Java lambda expression as method argument example there is a method invoke that has a Callable as argument. Since Callable is a functional interface so a lambda expression compatible to it is passed as a parameter while calling invoke method.

Output

Block lambda expressions in Java

What we have seen till now are all single expressions but you can also have lambda expressions having multiple statements known as block lambda expressions.

In a block lambda expression enclose the block of code with in curly braces and explicitly use return to return a value.

Output

That’s all for the topic Lambda Expressions in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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.