Lambda expression

  • Eclipse: Oxygen
  • Java: 1.8

The lambda expression is used mainly to execute programs making the programs shorter. Therefore, previously we used classes to implement functions defined in the interfaces, but here we can directly write the function declarations with the help of the lambda expression.

In the following program, we demonstrate how to create a lambda expression with a single statement.

To create a lambda expression, we specify the input parameters (if any) on the left side of the lambda operator ->, we place the expression of the declarations on the right side of the lambda operator.

Output

Statement 1

In the following program, we demonstrate how to create lambda expression with multiple statements.

To create multiple lambda expression, we use curly braces {}, and the return keyword is required if the lambda expression is expected to return a value.

Output

Statement 1

Statement 2

Lambda expression continues

We can make use of lambda expression for functional type interfaces only.  An interface with the only a single abstract method is called a functional interface.

Here the lambda expression on threads is explained. As we know, we can have created threads with the Runnable method and, therefore, this could be done more efficiently with the help of lambdas.

The following program demonstrates the Runnable interface without using the Lambda expression.

Output

I am inside thread 1

The following program demonstrates the Lambda expression, instead of creating a Runnable interface, we can create a lambda expression inside the main method.

Output

I am inside thread 1

Here is the same example of lambda expression with multiple statements.

Output

I am inside thread 1

This is line 2

Lambda expressions, something more

The lambda expression is explained here with the Collections in java. A couple of methods such as the compareTo method and many more are written shortly and efficiently with the help of lambda expression.

Here are some lambda and collection examples, which demonstrates how to use a lambda expression in place of the comparator object. It provides an efficient and concise way to iterate, and fetch data.

We can use the Comparator interface for sorting elements of the list, it contains one abstract method called compare() method.

This is the example of sorting collection with a comparator.

The following program demonstrates how to iterate lambda expression over a List which helps us to concise our program.

Here we are using a lambda expression in place of comparator object for defining our sorting in collections. To create multiple lambda expression, we use curly braces {} and use return keyword.

Output

Angelica

Chaand

Ed

John

Maqbul

The following program demonstrates the use of lambda expression to perform some custom operations.

Output

Ed

John

Chaand

Maqbul

Angelica

Lambda expressions, few more thing

This method is a descriptive summary of all the things we have covered so far in the previous module. In the following sections, you will learn how variables work within lambda expressions.

The body of a lambda expression has the same scope as the Anonymous class.

The following program demonstrates lambda expression for a single parameter.

Here method variable will be able to access inside the lambda.

Output

The value of x is: 201

Statement from demo method

This program demonstrates lambda expression for two parameters. This expression implements Lambda interface.

If we have only one statement in our method then we place the expression of the declarations on the right side of the lambda operator.

With the help of lambda expression, you can perform an operation. For example Lamda lamda = () -> (10*25);

Lambda expressions with variables and iterations

The use of lambda in for each loop is shown and how it is efficiently used than the traditional for each loop is given here.

Output

Chaand

John

Mia

Angelica

Roy

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu