What are Lambda Expressions in Java 8?

AlishaS
Javarevisited
Published in
8 min readJan 25, 2023

The lambda expression is a shorthand syntax or we can say that is a short block of code that takes the parameters inside it and returns the result/value. The lambda expression can also be said that they are concise ways to write a function.

Some of you might be familiar with the programming languages JavaScript and typescript, and you might have used the arrow function or the fat arrow symbol. So similarly the arrow function or the fat arrow function is known as lambda expressions in the java programming language.

We can use the lambda expression when we want our code to be short and when we do not want to use the word function several times in our code.

Scope of the article

  • In this article, we will read about lambda expressions in detail with proper examples.
  • With the help of proper examples, we will see how to implement the lambda expression in the java programming language.
  • We will also see how using the lambda expression in java we can reduce the length of the code which will make the code easier to read and understand.

Introduction

As we have already studied about lambda expressions, the lambda expression can also defined as a concise way to write a function in java that is similar to the Arrow or the fat arrow function in javascript or typescript.

With the help of lambda expression in java we do not need to define the method again for providing the implementation of the code we can directly write or imply the code. The lambda expression in java provides the implementation of the functional interface.

The interface which has only one abstract method is called a functional interface in java. The lambda expressions in java are just like functions and they also accept the parameters just like the functions the main difference is that in the lambda expression, we avoid writing the word function every now and then in our code and simply use the lambda expression which makes our code more readable and easier to understand.

Now let us see the syntax of the lambda expression in java:

Basic lambda experssion syntax in java:

(the lambda expression with the argument list) ->

{

body

}

This is the basic representation of how we can implement the lambda expression in java.

Now we will see the syntax of how we can implement the lambda expression with no parameters, with only one parameter, and with multiple parameters in java:

Syntax- Lambda expression with no parameters inside in it:

()->{

// In this example no parameters are passed inside the fucntion.

}

In this syntax, we are able to see that no parameters are passed that is the body is of no parameter lambda.

Now we will see the syntax of the lambda expression with only one parameter inside it:

Syntax- Lambda expression with only one parameter inside it:

(par1) -> {

//In this syntax we can see that the body consists of only one single parameter lambda.

}

In this syntax, we are able to see that only one parameter is passed that is the body is of only one parameter lambda.

Now we will see the syntax of the lambda expression with multiple parameters inside it:

Syntax- Lambda expression with multiple parameters inside it:

(parameter1,parameter2) -> {

//In this syntax we can see that the body consists of multiple parameter lambda that is the Body consists of multiple parameter lambda

}

In this syntax, we are able to see that only multiple parameters are passed that is the body consists of multiple parameter lambda.

Now let us see a few examples of how to implement the various lambda functions with and without parameters in our code.

First, let us see an example where we will not use the lambda function expression in our code instead we will use the function keyword so that it becomes easy to differentiate:

Example without using the lambda expression in java:

interface calculate{

public void print();

}

public class example {

public static void main(String[] args) {

int a=10;

//Without the use of the lambda expression in java we are implementing using the calculate class

calculate c=new calculate(){

public void print(){

System.out.println(“The result of the following expression is “+a);

}

};

c.print();

}

}

In the above example we are not using the lambda expression, we are using the calculate class instead.

Now let us see an example in which we will implement the lambda expression so that we can understand the difference:

Example using the lambda expression in Java

interface calculate{

public void print();

}

public class example {

public static void main(String[] args) {

int a=12;

//With the use of the lambda expression in java we are implementing

calculate c=()->{

System.out.println(“The result of the following expression is “+a);

};

c.print();

}

}

Output

12

As we can see in this example using lambda expression in java we are implementing the above example, using the lambda expression the size of the code has reduced. Run the above code in your editor for a better and clear explanation.

Now let us see another example of the lambda expression in java:

import java.util.*;

public class Main {

public static void main(String[] args) {

ArrayList<Integer> arr = new ArrayList<Integer>();

arr.add(12);

arr.add(93);

arr.add(84);

arr.add(67);

arr.add(65);

arr.add(09);

arr.add(42);

arr.add(23);

arr.forEach( (n) -> { System.out.println(n); } );

}

}

Output

12

93

84

67

65

09

42

23

In the above example we have seen a Java Lambda Expression Example using the For each Loop where we have first created an array list then added a few elements in the array list then using the for each loop in java we have iterated over the values and printed the values. Run the above code in your editor for a better and clear explanation.

Now let us see the examples with no parameters passed, only one single parameter passed and multiple parameters passed using the lambda function in java:

Java lambda expression when no parameters are passed:

interface calculate{

public void print();

}

public class example {

public static void main(String[] args) {

//With the use of the lambda expression in java we are implementing

calculate c=()->{

return “Lambda expressions are very useful in java”;

};

System.out.println(c.print());

}

}

Output

Lambda expressions are very useful in java

In the above example, we have not passed any parameters inside the function. The lambda expression is passed without any parameters being passed to the functional interface calculate. Run the above code in your editor for a better and clear explanation.

Now let us see an example in java when no parameters are passed inside the lambda function in java:

Java lambda expression when only a single parameter is passed inside the function:

interface calculate{

public void print();

}

public class example{

public static void main(String[] args) {

// The above example is the lambda expression with only a single parameter passed inside it.

calculate c=(a)->{

return “Lambda expressions are very useful in java “+a;

};

System.out.println(c.print(“The lambda expression is a short hand syntax or we can say that is a short block of code which takes the parameters inside it and returns the result/value”));

// You can omit function parentheses

calculate c2=(a) ->{

return “The lambda expression is a short hand syntax or we can say that is a short block of code which takes the parameters inside it and returns the result/value. “+a;

};

System.out.println(c2.print(“The lambda expressions in java are just like functions and they alo accept the parameters just like the functions”));

}

}

Output

* Lambda expressions are very useful in java, The lambda expression is a shorthand syntax or we can say that is a short block of code that takes the parameters inside it and returns the result/value

* The lambda expression is a shorthand syntax or we can say that is a short block of code that takes the parameters inside it and returns the result/value, The lambda expressions in java are just like functions and they also accept the parameters just like the functions

In the above example, we have only passed a single parameter inside the lambda function and returned the passed parameter in the return statement. Run the above code in your editor for a better and clear explanation.

Java lambda function when multiple parameters are passed inside it:

Let us see an example of a java lambda function when multiple parameters are passed inside it.

interface calculate{

int sum(int n1,int n2,int n3);

}

public class example{

public static void main(String[] args) {

// Example of the lambda function when Multiple parameters are passed inside the lambda expression

calculate c1=(n1,n2,n3)->(n1+n2+n3);

System.out.println(c1.sum(13,23,45));

// In the below line we are passing multiple parameters with data type in the lambda expression

calculate c2=(int n1,int n2,int n3)->(n1+n2+n3);

System.out.println(c2.sum(40,65,78));

}

}

Output

79

183

In the above example using the lambda function in java, we have passed several parameters using and then calculated the sum of the parameters passed. Run the above code in your editor for a better and clear explanation.

Now let us see another example using lambda in which we will see both the cases that is using the return keyword and without using the return keyword in java:

Java lambda function expression with or without using the lambda expression:

interface calculate{

int sum(int n1,int n2);

}

public class example {

public static void main(String[] args) {

// Example of the Lambda expression without using the return keyword.

calculate c1=(n1,n2)->(n1+n2);

System.out.println(c1.sum(56,34));

// Example of the Lambda expression with and using the return keyword.

calculate c2=(int n1,int n2)->{

return (n1+n2);

};

System.out.println(c2.sum(14,230));

}

}

Output

90

244

In the above example, we have both cases with and without using the return keyword in java, in the first case we have not used the return keyword, and in the second case we have used the return statement and passed the parameters inside the function. Run the above code in your editor for a better and clear explanation.

Now let us see an example of the java lambda expression to create threads in java:

Java lambda expression to create threads in java:

public class example{

public static void main(String[] args) {

//Thread Example without lambda expression in java

Runnable r1=new Runnable(){

public void run(){

System.out.println(“Java lambda expression to create threads in java”);

}

};

start s1=new start(r1);

s1.start();

//Thread Example with lambda expression in java

Runnable r2=()->{

System.out.println(“Java lambda expression to create threads in java”);

};

start s2=new start(s2);

s2.start();

}

}

Output

Java lambda expression to create threads in java

Java lambda expression to create threads in java

In this example, we have seen how we can create threads in java using the lambda expression. Run the above code in your editor for a better and clear explanation.

Use cases of or advantages of the lambda expressions in java:

The lambda expressions are added in JAVA 8. The lambda expression is a shorthand syntax or we can say that is a short block of code that takes the parameters inside it and returns the result/value. The lambda expression can also be said that they are concise ways to write a function.

Now let us read about some of the uses of lambda expressions in java 8:

  • We have to write fewer lines of code using the lambda expression in java, as we do not have to write the function word several times in our code.
  • By using the stream APIs and lambda expressions we can achieve higher efficiency (parallel execution) in case of bulk operations on collections.
  • The lambda function in java allows us the use of functional programming.
  • The lambda function in java increases innovations and it is a huge support for developers.

Conclusion

  • The lambda function is a concise way to write your code in java.
  • Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
  • The lambda expressions in java can only be used with a functional interface as then only we can use the lambda expressions with convenience.
  • The lambda function does not need to have a return type.
  • Using the lambda expression in java makes the code easier to read and understand.

--

--

AlishaS
Javarevisited

I am enthusiastic about programming, and marketing, and constantly seeking new experiences.