Lambda Expressions in Java 8

Lambda expression are java way to support first class functions. First class function means, function is first class citizen. In a programming language First class citizen (Function) is an entity, which
1) can be passed as a parameter to another function,
2) can be returned as a result from a function,
3) can be assigned to a variable,
4) can be stored in data structures.

In java method is a function, but it is not a first class citizen. As we can’t pass method as a parameter, you cannot return it from functions, you cannot assign it to a variable and last you cannot store it into data structures. In functional style programming we use function as an object. By using Functional style programming our code become more readable, reusable, testable, maintainable and concurrent.

Lambda expressions in Java introduce the idea of functions into the language. In conventional Java terms lambdas can be understood as a kind of anonymous method with a more compact syntax that also allows the omission of modifiers, return type, and in some cases parameter types as well.

Lambda Expression Syntax: The basic syntax of a lambda is 

which can be interpreted by below syntax examples

Lambda Expressions Syntax Notes:

  • The body may be a block (surrounded by braces, ex. 6) or an expression (ex. 1-5). A block body can return a value (value-compatible, ex. 6) or nothing (void-compatible). The rules for using or omitting the return keyword in a block body are the same as those for an ordinary method body.
  • If the body is an expression, it may return a value (ex. 1,2,3,5) or nothing (ex. 4)

Lambda expressions syntax have a type and are assignable. So when we say

This lambda expression has a type and the type is a Functional Interface. We can therefore assign a lambda expression to a functional interface, and any expression that is assignable to a functional interface can be used as a lambda expression.  So we can assign our above lambda expression, which doesn’t take any argument and return no value to a Runnable interface (which is Functional interface as it have only one method run, which takes no parameter and returns no value)

                                             

1)  A basic String Comparator Lambda Expressions Example:

Basically comparator is an object in java, which compare 2 objects. In above example we have comparator of String, which can compare 2 string characters between themselves. The Comparator interface having only one abstract method public int compare. Which takes 2 objects as an arguments, which need to be compared and it implements the logic of two objects how they should be compare. Here for String objects we are comparing on the basis of there lengths. Here our comparator object is an instance of anonymous class.

We can use this it to sort an array of Strings.

We have sort method on the Arrays factory class. Where stringArray is an array of String. The sort method internally Quick Sort and sort elements using comparator.

What we did in the above programme?

We write up some code in anonymous class, and we build in instance of this anonymous class. Finally we passed that instance to another piece of code, basically the sort method in above case.  And this code which we passed is executed later (this is same way of doing using JavaScript closure).

In above example we passed code as a parameter. We passed our logic of comparison for string. The anonymous  class is only way to do that (passing code as parameter) in java. The notion of anonymous class or instance of anonymous class is passing code as parameter in java. Many use case are there like Swing, GUI etc in java. See anonymous class for reference.

Let’s write above code in anonymous class code in lambda expression way in java.

A Lambda expression is another way of passing our code as parameter, so it can be executed later, just once or multiple times.

The comparator code we wrote above, can be written in lambda expression as following:

Note: The target type of lambda expression is Functional interface.  In our example Comparator is functional interface in java8.

2) Runnable interface Lambda Expression Example: Runnable interface which having a single method public void run()

How we can use this runnable object, we can pass this to another

What we did do so far?

We write up some code in anonymous class, and we build in instance of this anonymous class. And we passed that instance to another piece of code. Basically the Executor Service which is pool of thread in the second case. And this code which we passed is executed later. In the this example it executed in another context means in another thread. In both example we passed code as a parameter. We passed our logic of comparison for string or some task to execute by another thread.

Lets us rewrite our code lambda expression way for our runnable. As we know Lambda expression is another way of passing our code as parameter.

 

Let see one more simple example for our Runnable

output :

 

In Runnable lambda expression, notice that no parameter is passed and is returned. The Runnable lambda expression, which uses the block format, converts five lines of code into one statement.

Note: The target type of lambda expression is Functional interface.  In our example  Runnable is functional interface as its SAM interface (contains single abstract method).

Why Lambda Expressions

The lambda expressions allows us to pass our code logic in a compact way as you have seen in above examples.

 

Leave a Reply

Your email address will not be published. Required fields are marked *