Lambda expression with the return statement

Here is the table content of the article will we will cover this topic.
1. What is the return type of lambda expression?
2. without return statement?
3. with return statement?

return type of lambda expression

What is the return type of lambda expression?

As you know Lambda expression supported the Functional interface. It creates a reference for the interface and provides the body with the abstract method. We have seen many examples with a lambda expression.

Now we will discuss how Lambda expression works with the return statements. Let’s say we have an abstract method in any Functional interface. The abstract method is declared with the return type.

public int show(int a);

So, when we provide the body with the method, we must take care of its return type. To return the value you can use a return statement.

We can return the value in two ways. So, we will discuss it with two scenarios.  

Without return statement

If you have only one line of code in Lambda expression and you are not using the curly braces ({ }). Then you don’t need to return the statement. You just need to write the object/variable which you want to return.

public interface Display 
{
	public int show(int a);
}
public class ExampleWithLambda
{
	public static void main(String[] args) 
	{
		Display display = (int a) ->  a;  	
		System.out.println("Returns value from lambda expression = "+display.show(5));
	}
}

Output: Returns value from lambda expression = 5

With return statement

If you have more than one line of code in Lambda expression and you are using the curly braces ({ }). Then you need to return the statement. You need to write the object/variable with the return statement which you want to return. In this example, we are using curly braces so we must use return statements.

public interface Display 
{
	public int show(int a);
}
public class ExampleWithLambda
{
	public static void main(String[] args) 
	{
		Display display = (int a) -> { return a; }; 	
		System.out.println("Returns value from lambda expression = "+display.show(5));
	}
}

Output: Returns value from lambda expression = 5

Returning a user-defined type from a lambda expression

Let’s discuss it with the user-defined class or real-time example:

interface Display 
{
      public List<Student> show();
}
class Student 
{
	int rollNo;
	String className;
	String name;
	public Student(int rollNo, String className, String name) {
		this.rollNo = rollNo;
		this.className = className;
		this.name = name;
	}
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ExampleWithoutReturn
{
	public static void main(String[] args) 
	{
		ArrayList<Student> listOfStudents = new ArrayList<Student>();
		listOfStudents.add(new Student(3, "MCA", "Ram"));
		listOfStudents.add(new Student(7, "BCA", "Sham"));
		listOfStudents.add(new Student(1, "BSC", "Krishana"));
		Display display = () -> listOfStudents.stream().filter(student -> student.getRollNo() < 5).collect(Collectors.toList());	
		List<Student> list = display.show();
		list.forEach(s -> System.out.println("Name of Student:"+s.getName()));
	}
}

Output: Name of Student:Ram
Name of Student:Krishana

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ExampleWithReturn
{
	public static void main(String[] args) 
	{
		ArrayList<Student> listOfStudents = new ArrayList<Student>();
		listOfStudents.add(new Student(3, "MCA", "Ram"));
		listOfStudents.add(new Student(7, "BCA", "Sham"));
		listOfStudents.add(new Student(1, "BSC", "Krishana"));
		Display display = () -> {
			return listOfStudents.stream()
			.filter(student -> student.getRollNo() < 5)
			.collect(Collectors.toList());	
		};
		List<Student> list = display.show();
		list.forEach(s -> System.out.println("Name of Student:"+s.getName()));
	}
}

Output: Name of Student:Ram
Name of Student:Krishana

Frequently Asked Questions

What is the return type of a lambda expression?

The return type of a lambda expression is determined by the abstract method of the functional interface it implements. If that abstract method declares a return type, the lambda body must produce a compatible value.

Do I always need a return statement inside a lambda expression?

No. If the lambda body is a single expression without curly braces, you just write the value to return. If you use curly braces with more than one line of code, you must use an explicit return statement.

Why does a lambda with curly braces require a return statement?

Curly braces turn the lambda body into a block. In a block body, the value is not returned implicitly, so you need to write the return statement yourself.

Can a lambda expression return a user-defined object or a collection?

Yes. As shown in the examples on this page, a lambda can return a user-defined type such as a list of Student objects, using either the expression form or the block form with a return statement.

What happens if the lambda body does not match the declared return type?

The lambda must be compatible with the abstract method of the functional interface. If the value produced by the body does not match the declared return type, the code will not compile.

3 thoughts on “Lambda expression with the return statement”

  1. It is incredibly, very user-friendly and user friendly. Great job for the fabulous site.

    Reply
  2. Very good written story. It will be useful to everyone who usess it, including me. Keep up the good work - i will definitely read more posts.

    Reply
  3. Crystal clear explanation. Thanks. varnan

    Reply

Leave a Comment