9/14/2022
Lambda Expression
• an unnamed block of code (or an unnamed function) with a list of formal
parameters and a body. Sometimes called a lambda.
• The body of a lambda expression can be a block statement or an expression.
• describes an anonymous function.
• An arrow (->) is used to separate the list of parameters and the body.
Syntax for Lambda Expressions
() -> { } // Takes no parameters and returns void
// Takes an int parameter and returns the parameter value incremented by 1
(int x) -> x + 1
// Takes two int parameters and returns their sum
(int x, int y) -> x + y
// Takes two int parameters and returns the maximum of the two
(int x, int y) -> { int max = x > y ? x : y; return max; }
() -> "OK" // Takes no parameters and returns a string "OK"
// Takes a String parameter and prints it on the standard output
(String msg) -> { System.out.println(msg); }
// Takes a parameter and prints it on the standard output
msg -> System.out.println(msg); //(msg)->System.out.println(msg);
The parentheses can be omitted only if the single parameter also omits its type.
// Takes a String parameter and returns its length
(String str) -> str.length()
A lambda expression is not a method, although its declaration looks similar to a
method.
As the name suggests, a lambda expression is an expression that represents an
instance of a functional interface.
1
9/14/2022
(String str) -> str.length()
Type of this lambda expression can be any functional interface type with an
abstract method that takes a String as a parameter and returns an int.
The following is an example of such a functional interface:
@FunctionalInterface
interface StringToIntMapper { int map(String str); }
The lambda expression represents an instance of the StringToIntMapper functional
interface when it appears in the assignment statement, like so:
StringToIntMapper mapper = (String str) -> str.length();
conforms to the declaration of the map()
type compiler finds lambda expression.
• It infers that the type of the lambda expression is the StringToIntMapper
interface type.
• When the map() method is called on the mapper variable passing a string, the
body of the lambda expression is executed as shown in the following snippet of
code:
StringToIntMapper mapper = (String str) -> str.length();
String name = "kristy";
int mappedvalue = mapper.map(name);
System.out.println("name=" + name + ", mapped value=" + mappedvalue);
StringToIntMapper mapper = new StringToIntMapper() {
@Override
code uses an anonymous class to
public int map(String str) {
achieve the same result as the lambda
return str.length(); } };
expression.
String name = "Kristy";
int mappedValue = mapper.map(name);
System.out.println("name=" + name + ", mapped value=" + mappedValue);
2
9/14/2022
Lambda Expression Equivalent Method
(int x, int y) -> { return x + y; } int sum(int x, int y) { return x + y; }
(Object x) -> { return x; } Object identity(Object x) { return x; }
(int x, int y) -> { if ( x > y){ return x; } int getMax(int x, int y) { if ( x > y) { return
else { return y; } } x; } else { return y; } }
(String msg) -> { System.out.println(msg); void print(String msg) {
} System.out.println(msg); }
() -> { void printCurrentDate() {
System.out.println(LocalDate.now()); } System.out.println(LocalDate.now()); }
() -> { } void doNothing() { }