Lambda Expressions: Concepts
Lambda Expressions: Concepts
Concepts
Functional Interface
Lambda Expression
Introduction
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 }
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
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
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
Here,
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.
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.
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
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
Sum interface that returns the sum of two numbers if the sum is a positive
integer, otherwise, it returns 0 .
Summary
Lambda Expression:
Functional Interface:
A functional interface is an interface that has a single abstract method
(SAM).