Start working with Java LAMBDA Expression

banner

What we will see in this blog about Lambda Expression:

  • 1. What is Lambda Expression in Java (Lambda Expression Syntax and details about it)?
  • 2. Difference between normal method and Lambda Expression
  • 3. Where to use Lambda expression in Java?
  • 4. Why is it introduced in Java 8 / Lambda expression advantage?
  • 5. Lambda expression usage on most of the commonly used Collection API implantation classes (Arraylist and HashMap).

What is Lambda Expression?

Lambda expression is a new feature introduced in Java 1.8 version. It is an anonymous function.

What is an anonymous function?

It is a method that will not have any name, can accept one or more parameters or even no parameters. Anonymous functions don’t have return types too.

Lambda expression is an anonymous function.

Syntax of Lambda Expression

(parameters) -> {function body}

Here we are specifying the parameters on the left side of the Lambda operator (->) and the statements block or can call as function body, which should specify on the right side of the Lambda operator(->).

Examples:

(x, y) -> x + y (int a , int b) -> {return a +b}

Difference between normal method and Lambda Expression:

Normal Method in Java

Lambda Expression in Java

The normal method has a name and a return type Lambda expression is also a method which doesn’t have any name or return type
The normal method can belong to a class or the instance of a class Lambda expression does not belong to a class. As there is no way to put a static reference to it.
Have no certain target type as such Lambda expression target type is Functional Interface. As well as Lambda expression can pass as an argument to the normal method.
Any type of access specifier private/public/default No access specifiers.

The normal method we use to write the code as follows:

Public String getName(String str) { return str; };

Lambda Expression code example:

Thread t1 = new Thread (() -> {System.out.print(“with Lambda Expression)}; (x ,y) -> x + y

Where to use Lambda expression in java?

To use Lambda expression in Java, either we have to create our functional interface or have to use the predefined functional interface present in Java. A functional interface is an interface that used to contain only one abstract method (we will have a detailed discussion about this in my later post). Some of the predefined functional interfaces present in java are Runnable, Callable, ActionListener, etc.

Why it originally introduced in Java 8 here, we will discuss what problem it is trying to solve.

So there will be a question in your head why Lambda expression introduced in Java and what are the problems it is trying to overcome.

The first problem before Lambda Expression was if you have to use Functional Interface you have to define an anonymous inner class in our code, which will decrease the code readability and also we have to introduce an extra line of codes.

But after Lambda expression is introduced in Java 1.8, there is no need for defining an anonymous inner class to execute the functional interface and override the predefined abstract method of the interface. Because all Lambda expressions converted into a functional interface type.

Code Examples:

Without Lambda Expression:

Code with Runnable interface, in the below example I am creating a Thread class, and assigning the Runnable anonymous inner class object as a target to the Thread class object, in Runnable object we have got the overridden run method where we are trying to print “Without Lambda Expression Example”.

And then calling the th.start() method, which will start the thread and print the above message.

Here we have to write extra lines of codes, and also we need to override the run method for our code execution, which is a cumbersome approach so to overcome that in Java 1.8 Lambda expression was introduced.

Lambda

With Lambda Expression

Now we will see the same above code got modified by using Lambda expression.

Lambda

We can easily visualize the difference by comparing both the codes. It is obvious by using Lambda's expression, our task of writing the logic in terms of code eased out.

Lambda Expression example with custom functional Interface

Code:

Steps:

  • 1. First, we have to declare our custom interface with only one abstract method.
  • 2. We have to use @FunctionalInterface annotation to mark the interface as functional Interface.
  • 3. Create an Implementation class
  • 4. In the Main method, call the Lambda expression with our custom interface as its target and hence returns the string message Good morning!
  • 5. Do not be confused, the return is not for the Lambda expression, as we have already discussed the Lambda expression does not have a return type. This return type is the functional interface type as the abstract method that we have designed in the interface accepts String as a return type.
Lambda
Lambda

Lambda expression usage on most of the commonly used Collection API implantation classes.

We will see the usage of the Lambda expression with ArrayList, HashMap implementation classes of the collection API.

As this both, the implementation classes are the most commonly used.

Code of Lambda expression with ArrayList implementation class.

Lambda

Code of Lambda expression with HashMap implementation class.

Lambda

Conclusion

So from the above discussion, the important concepts what we have to keep in our mind are.

  • Lambda expression in java is a method with doesn’t have a name and a return type. It can have a parameter or can be executed without parameter too.
  • Lambda expression target type is Functional Interface or in the easy word always converted into a functional interface type.
  • We can also pass a Lambda expression as an object to a method.
  • We can use it with the Collection and Stream API of Java Application Development services. And our job is to create logic through the terms of code introduced very easily.
  • It increases the readability of the code.
  • Less code we have to write.
  • Easy usage with each loop, for iteration.
Empower your Business with Team Aegis, CONNECT NOW!
captcha

Scale your Business with our Software Services Now!
Related article

Cloud computing has revolutionized the IT industry by providing infrastructure as a service. Companies/Individuals can launch their servers on a cloud without purchasing single hardware, moreover, they don’t have to spend a single penny on maintenance and administrations. Servers can be launched on AWS cloud in no time and you have to pay for what you use.

In this day and age of advanced technology, it is almost difficult to survive without internet access and the presence of online businesses.

Java is one of the world’s most famous languages. It is regarded as one of the top languages in the world simply because it makes the life of the programmers easy.

DMCA Logo do not copy