Lambda Expression Asslam
Lambda Expression Asslam
JAVA 8 –
Lambda Expressions
for SDETs
Lambda expression is a new feature introduced in Java 8. It has brought functional programming capabilities to JAVA and is one
of the most popular features of Java 8. Lambda expressions are used to create instances or objects of functional interfaces. A
functional interface is an interface with a single abstract method (we already have interfaces like Runnable, Callable,
ActionListener, etc.). A lambda expression can be expressed as an anonymous function with no name and doesn't belong to any
class.
We can directly write an implementation for a method by using lambda expressions. In this case, the compiler doesn't create a
specific class file for these expressions; it executes lambda expressions as a function. We can also manipulate collections such
as performing iteration, extracting and filtering data, etc.
(Parameters) → (Expression);
For Example, the lambda expression (x, y) → x + y specifies that lambda expression takes two arguments x and y and
returns the sum.
Before Java 8
Interface with an abstract method
02
To execute the abstract method, created a class and
implemented the interface public class TestExecution implements add {
@Override
Output:
public void sum(int a, int b) {
The sum is:: 30
int c = a+b;
System.out.println("Sum is :: "+c);
}
public static void main(String[] args) {
TestExecution ts = new TestExecution();
ts.sum(10, 20);
}
}
Note:
2. Functional Interface annotation allows a single abstract method. If another abstract method/non-abstract method is created, it
throws a compilation error in the imple mentation, and lambdas are not supported.
Schedule a FREE
Consultation.
03
Lambda Expressions are of Single
Line and Multi-Lines
If it is a multi-line lambda, then we can use braces and ends with a semi-colon as
shown below
Here (x,y) is the parameter and System.out.println("Sum is :: "+ (x+y)) is the expression
Output:
@FunctionalInterface
public interface EmployeeDetails {
Name:: John Watson
04
Returning Values from Lambda
Interface with the abstract method of String return type
Output:
@FunctionalInterface SELENIUM
public interface EmployeeDetails {
The above Example returns the value according to the function
String empDetails(String firstName);
mentioned in the Lambda.
}
public class TestExecution{ As the above Example is a single line lambda with a return
public static void main(String[] args) { type, we can remove the
EmployeeDetails emp = (fn) -> {
return keyword and write lambda expression as,
return fn.toUpperCase();
};
System.out.println(emp.empDetails("Selenium"));
EmployeeDetails emp = (fn) -> fn.toUpperCase();
}}
Functional Programming
Functional Programming is a way of building software by using pure functions and avoiding shared state and side effects. The
shared state is a variable being accessed by more than one function, and side effects are nothing but a function modifies the
variable.
f(x) = x+3
x is the input, and x+3 is the output. This is nothing but a lambda expression.
For Example,
Pure Function
Higher-Order functions
05
Pure function: Consistent, predictable output for the given input
f(x) = x+3
This is a pure function because, for a given input, the output is consistent and known.
If x = 3, output is 6
If x = 5, output is 8
The below lambda expression is an example of pure function. The output is based on the given input.
Input – Selenium
Output - SELENIUM
For example,
f(x) = x+3+y
This is an impure function because the output is based on the outside variable 'y.'
If x = 3, the output is 6 + y. The output is inconsistent and depends on 'y' variable, which is not part of the function.
@FunctionalInterface
public interface EmployeeDetails {
Output:
SELENIUMLinux
The output is inconsistent and is dependent on the list object. If we change the index of the get() method in the above Example,
then the output will be different.
06
Function as a first-class object:
For Example,
Output:
public interface EmployeeDetails {
SELENIUM
void empDetails(String firstName);
}
In the above Example, to execute the abstract method, we are
implementing the interface and then calling the method with
public class TestExecution implements EmployeeDetails{ the help of a class object as reference.
Output:
@FunctionalInterface
public interface EmployeeDetails { SELENIUM
Higher-Order Functions
@FunctionalInterface
public interface EmployeeDetails {
A method that receives another function as a parameter
is called a higher-order function. String empDetails(String firstName);
}
Let us see with an example, public class TestExecution{
Output:
public class TestExecution{
PRAMOD
public static void main(String[] args) {
test(s -> s.toUpperCase());
6
test((a) -> String.valueOf(a.length()));
}
private static void test(EmployeeDetails emp) {
String name = emp.empDetails("Pramod");
System.out.println(name);
}
}
From the above Example, it is clear that the behavior is given inside a method, and the variable is passed as a reference. If we want
to change the behavior, we need to create another method.
convertUppercase("Selenium");
convertLowercase("Java");
}
08
Here, we have created another method with behavior where it converts a String to lowercase. And to execute, we are passing a
variable as a reference. Let's create a higher-order function
In higher-order function, the variable is present inside the method, and the behavior is passed as a reference. If we want to execute
the method with another behavior, we need not create another method; instead, we can pass the behavior as a reference.
Here the test() method returns the lambda expression. We can also write the return statement as:
09
Method References
Method references are another way of writing lambda expressions; usually, it is beneficial for single line lambda expressions,
which call existing methods. It is used to refer to the method of functional interface. The main advantage of using method
references is it is easy to read and compact.
For Example
} op1.accept("data");
} Output:
}
data
The above code is an example of a standard way of creating a lambda expression. As mentioned before, Method reference is
another way of writing a lambda expression. Whatever input is given, the output is printed by using the variable's'. And we know
System.out.println is a static method because println can be called without creating an object. So, here we are trying to invoke
another method to use the variable's'. Method references are used only for single lambda expressions. So, we can rewrite the
lambda expression as:
Java understands that the given functional interface will accept only one
public class TestExecution { variable, and System.out::println is a lambda expression using Method
ClassName::StaticMethodName
Example 1
10
Output:
In the above Example, we have defined a functional interface and referred to the static method 'stringDisplay()' to its functional
method 'accept().'
The static method is called by its class name, and the lambda expression is written in a method reference format. The lambda
expression behavior is stored in the 'op1' object, and by using this object, we are calling the accept() method.
Example 2
Output:
public class TestExecution {
public static int add(int a, int b){ The sum is:: 30
return a+b;
} In this Example, we have used a predefined functional
public static void main(String[] args) { interface, i.e., BiFunction, and by
BiFunction<Integer, Integer, Integer> sum = TestExecution::add;
int result = sum.apply(10, 20); using its apply() method, we can execute the lambda
System.out.println("Sum is :: "+result);
expression behavior.
}
}
Syntax:
ClassObjectName::InstanceMethodName
Example 1
Output:
In the above Example, we have defined a functional interface and referring the non-static method 'stringDisplay()' to its
functional method 'accept().'
The non-static method is called by its class object, and the lambda expression is written in a method reference format. The
other way of doing it is by directly calling the class's
11
Example 2
Output:
public class TestExecution {
The sum is:: 30
public int add(int a, int b){
return a+b; In the above Example, we have used a predefined
}
functional interface, i.e., BiFunction, and by using its
public static void main(String[] args) { apply() method, we can execute the behavior of the
BiFunction<Integer, Integer, Integer> sum = new TestExecution()::add;
int result = sum.apply(10, 20); lambda expression.
System.out.println("Sum is :: "+result);
} As the add() method is a non-static method, it is
}
called by the instance of the class to write a lambda
expression.
3. Reference to a constructor
Syntax:
Classname/ConstructorName::new
Output:
data
In the above Example, we can refer to a constructor by using the new keyword. The constructor is directed with the help of a
functional interface.
Schedule a FREE
Consultation.
12
Miscellaneous Examples
} System.out.println(op1.accept("Ja","va"));
}
}
Output:
Java
In the above Example, we have defined a functional interface where it accepts two Strings and returns a String. The lambda
expression is a reference to a static method type
of method reference. The static method 'concat' is called by its class name 'String.'
Conclusion
With lambda expressions, the objective is to minimize many of the disadvantages of using a single class or anonymous inner
class when implementing a functional interface while at the same time maximizing the advantages. If you look at the above
examples implemented using lambda expressions in Java, there's no denying that the code looks much more concise and
compact.
These simple examples of lambda expressions show a few of their advantages in Java compared to other approaches. As you
explore more advanced levels, you will find how they make iterative processing easier using the new, functional 'forEach'
method. And because lambda expressions can be assigned to a variable, they provide an opportunity for reuse, which simply
isn't possible with anonymous inner classes.
References
1. https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
2. https://www.geeksforgeeks.org/lambda-expressions-java-8/
3. https://www.javatpoint.com/java-lambda-expressions
13
Join in
Partner with Jade
Let us guide your path amidst the clouds and help
you build the future enterprise.
Be an expert
Become a master in the interplay of domains and
industries. Drive higher benchmarks of success.
A leader
Knowledge powerhouse we all hold
together-clients, partners and Jade experts.
For more
information