0% found this document useful (0 votes)
191 views

L23. Lambda Built-In Functional Interfaces

The document discusses lambda expressions and functional interfaces in Java. It defines a functional interface as an interface with only one abstract method, and explains that lambda expressions provide an anonymous implementation of the abstract method of a functional interface. Examples are provided of lambda expressions implementing functional interfaces to add two numbers and filter strings by a given criteria.

Uploaded by

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

L23. Lambda Built-In Functional Interfaces

The document discusses lambda expressions and functional interfaces in Java. It defines a functional interface as an interface with only one abstract method, and explains that lambda expressions provide an anonymous implementation of the abstract method of a functional interface. Examples are provided of lambda expressions implementing functional interfaces to add two numbers and filter strings by a given criteria.

Uploaded by

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

Programming in Java

Topic: Lambda Expressions & Functional


Interfaces

By
Arvind Kumar
Asst. Professor, LPU
Functional Interface
 A functional interface is an interface that specifies only
one abstract method.

 Also known as Single Abstract Method (SAM) interfaces.

 Introduced with jdk8.

 @FunctionalInterface annotation can be used for


declaring a functional interface which results in an error
when contract of Functional Interface is violated.
Example:
@FunctionalInterface
interface MyInterface
{
int test(int n);
}

Note: If an interface declares an abstract method overriding one


of the public methods of Object class, that does not count
towards the interface’s abstract method.
Example:
@FunctionalInterface
interface MyInterface
{
int test(int n);
public boolean equals(Object ob);
public String toString();
}
Lambda Expression

 A lambda expression is, essentially, an anonymous (unnamed)


method which is used to implement a method defined by a
functional interface.

 Lambda expressions are implementation of only abstract


method of functional interface that is being implemented or
instantiated anonymously.
Fundamentals of Lambda Expressions
 Lambda Expression introduces a new operator (−>) which is
referred to as the lambda operator or the arrow operator.

 It divides a lambda expression into two parts.

 The left side specifies any parameters required by the lambda


expression. (If no parameters are needed, an empty parameter
list is used.

 On the right side is the lambda body, which specifies the


actions of the lambda expression.
Example

int sum(int a, int b)


{
return (a+b);
}

Using Lambda:
(int a, int b) -> (a+b);
OR
(a, b) -> a+b;
Important
 When a lambda expression has only one parameter, it is
not necessary to surround the parameter name with
parentheses when it is specified on the left side of the
lambda operator.

Example:
boolean isEven(int n){
return (n%2==0);
}
Using Lambda Expression: n -> (n % 2)==0;
Structure of Lambda Expressions
Argument List:
 A lambda expression can contain zero or more arguments.

// No argument
( ) -> { System.out.println("No argument"); }

// Single argument
(int arg) -> { System.out.println(“One argument : " + arg); }

// More than one arguments


( int arg1, String arg2 ) -> { System.out.println(“Multiple
Arguments:); }
Structure of Lambda Expressions
Argument List:
 We can eliminate the argument type while passing it to lambda
expressions, those are inferred types. i.e. ( int a ) and ( a ) both
are same.
But we can not use inferred and declared types together
( int arg1, arg2 ) -> { … } // This is invalid

 We can also eliminate “()” if there is only argument.

 More than one arguments are separated by comma (,) operator.


Structure of Lambda Expressions
Body of a lambda expression:

 Body can be a single expression or a statement block.

 When the body consist of a single expression, it is called as


Expression Lambda or Expression Body.

 When the code on the right side of the lambda operator consists
of a block of code that can contain more than one statement, It is
called as block body or Block Lambda.
Characteristics of Lambda Expressions

 Optional type declaration


 Optional parenthesis around parameter
 Optional curly braces
 Optional return keyword
Use of Lambda expression

• Reduced Lines of Code

• Sequential and Parallel Execution Support


Let’s Do It?

 Create a functional interface Predicate with an abstract method


test with following signature:
boolean test(String t);
Write a test program to filter all the strings present in ArrayList
which ended with character ‘g’ by using above test method
with the help of lambda expression.
Brain Storming Questions

 Which of the following is/are the correct lambda expression


which add two numbers and return their sum?
 A - (int a, int b) -> a + b
 B - (a, b) -> a + b
 C - (int a, b) -> a+b
 D - (a, int b) -> a+b

 Ans- AB

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy