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 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

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

2 thoughts on “Lambda expression with the return statement”

  1. 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

Leave a Comment