0% found this document useful (0 votes)
8 views7 pages

Lambda Expressions: Concepts

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Lambda Expressions: Concepts

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lambda Expressions

Concepts
Functional Interface

Lambda Expression

Lambda Expression with Parameters

Types of Lambda Body

Introduction

In object-oriented programming, we have learned to achieve abstraction using


interfaces. We have also learned that interfaces cannot be instantiated. So, even to
implement an interface that has only one abstract method we have to create a class
and implement the abstract method in it.

Lambda Expressions are a new feature introduced in Java 8 to write concise and
more readable code, particularly when working with interfaces with a single abstract
method.

In this unit, we'll learn what are Lambda Expressions and write the code
concisely.

1. Functional Interface
A functional interface is an interface that has a single abstract method (SAM). A
functional interface can have any number of default methods (methods with a default
implementation) or static methods, but it must have only one abstract method.

Code
JAVA

1 interface Runner {
2 void run();
3 }

In the above code, the

Runner interface consists of only a single abstract method. Hence, it is called a


Functional Interface.

Now, let's say there is a class

BaseballPlayer that uses the run() method less number of times (let's say only
1 time). To use the run() method, the BaseballPlayer class has to extend any
other class which implements the Runner interface or it has to implement the
Runner interface and provide the implementation for the run() method like
below,

Code
JAVA

5 class BaseballPlayer implements Runne


6 public void run() {
7 System.out.println("Running"
8 }
9
10 public static void main(String[]
11 BaseballPlayer player = new
12 player.run();
13 }
14 }

Expand

To use the
run() method, we have implemented the Runner interface, created an instance
of the BaseballPlayer , and then called the run() method to execute the
functionality.

We can reduce all the unnecessary code and make our code more readable and
concise using Lambda Expressions.

2. Lambda Expressions

A lambda expression consists of a list of parameters, an arrow, and a body. They


provide a way to represent a function as an object

The parameters and body of a lambda expression are similar to those of a method.
The main difference is that a lambda expression does not have a name and does not
belong to a class. Instead, it is an anonymous function that can be passed around as a
value.

Syntax
JAVA

1 (parameters) -> lambda body

Here,

parameters : comma-separated values


-> : it is known as an arrow operator or a lambda operator
lambda body : consists of the code

Similar to methods, the lambda expressions need to be invoked.

Let's write the

BaseballPlayer class and provide implementation of the run() method using


the Lambda Expression.
Code
JAVA

1 interface Runner {
2 void run();
3 }
4
5 class BaseballPlayer {
6 public static void main(String[] ar
7 Runner ref = () -> System.out.p
8 ref.run(); // invokes the lambd
9 }
10 }

Output

Running

Note

Interfaces can be used for referencing. This means that you can create a
variable of an interface type and assign it a reference to an object that
implements the interface.

In the above code, the lambda expression represents the function as an object and
implements the

Runner interface. The statement ref.run() invokes the lambda expression and
the Running text is displayed on the console.

2.1 Lambda Expression with Parameters

Till now we have created lambda expressions without any parameters. The lambda
expressions can have parameters similar to methods.
Let's write a lambda expression that accepts parameters.

Code
JAVA

3 }
4
5 class Main {
6 public static void main(String[]
7 Sum ref = (a, b) -> a + b;
8
9 int result = ref.add(2, 3);
10 System.out.println(result);
11 }
12 }

Expand

Output

In the above example, the return type and parameter types of the lambda expression
are not explicitly provided.

The Java compiler determines it implicitly.

2.2 Types of Lambda Body

In Java, the lambda body is of two types:

1. Lambda body with a single expression


2. Lambda body consisting a block of code.

1. Lambda body with a single expression

Lambda expression consists of one expression as its body. This type of lambda
expression can be written in a more concise form because the curly braces and the
return keyword can be omitted.

Syntax
JAVA

1 (parameters) -> lambda body

2. Lambda body consisting a block of code

The lambda expression consisting a block of code is surrounded by curly braces. The
block of code can contain any number of statements.

Syntax
JAVA

1 (parameters) -> {
2 // lambda body
3 }

Code
JAVA

9 if (numSum > 0) {
10 return numSum;
11 }
12 return 0;
13 };
14
15 int result = ref.add(2, 3);
16 System.out.println(result);
17 }
18 }

Expand

Output
5

In the above code, the lambda expression implements the

Sum interface that returns the sum of two numbers if the sum is a positive
integer, otherwise, it returns 0 .

Summary

Lambda Expression:

A lambda expression consists of a list of parameters, an arrow, and a body.

Similar to methods, the lambda expressions need to be invoked.

Functional Interface:
A functional interface is an interface that has a single abstract method
(SAM).

Interfaces can be used for referencing.

You might also like