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

Day18 LambdaExpression

Java

Uploaded by

mohamedjaaved19
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)
7 views

Day18 LambdaExpression

Java

Uploaded by

mohamedjaaved19
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/ 26

Lambda Expression

TNS India Foundation | ‹#›


Partners in Economic Transformation
Recap

● Generics - It is declared in the “<>” brackets


● Generic class - It is designed to work upon objects. Hence, it cannot work
with primitive data types.
● Generic Method- A method that takes generic type parameters.
● Unbounded WildCard-An unbounded wildcard is represented by <?>.
● Lower Wildcard - It use the super keyword.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. What is the purpose of the diamond operator in Java generics?

A To specify a lower bound for a type parameter

B To specify an upper bound for a type parameter

C To specify both an upper and lower bound for a type parameter

D To allow for type inference in a generic class or method

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. What is the purpose of the diamond operator in Java generics?

A To specify a lower bound for a type parameter

B To specify an upper bound for a type parameter

C To specify both an upper and lower bound for a type parameter

D To allow for type inference in a generic class or method

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2.Which of these types cannot be used to initiate a generic type?

A Integer class

B Float class

C Primitive Types

Collections
D

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2.Which of these types cannot be used to initiate a generic type?

A Integer class

B Float class

C Primitive Types

Collections
D

TNS India Foundation | ‹#›


Partners in Economic Transformation
Learning Objective

● Understand the concept of a Functional Interface.


● Demonstrate the implementation of a functional interface through the creation of two
distinct classes.
● Define Lambda Expressions and provide an illustrative example.
● Different types of Functional Interfaces
● Explore the practical applications and significance of Lambda expressions.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Functional Interface

• An interface is declared with only one abstract method, then it is referred as Functional
Interface.

•The method in functional interface is called as functional method.

Along with the functional method, you can also add default and static method to functional
interface.

•Examples of Functional interfaces are:

•Runnable

•ActionListener

•ItemListener
TNS India Foundation | ‹#›
Partners in Economic Transformation
Hook

TNS India Foundation | ‹#›


Partners in Economic Transformation
Lambda Expression

•A lambda expression is an anonymous block of code that encapsulates an expression or a


series of statements and returns a result.
● Syntax: (parameters) -> { statements; }

•Lambda expression allows for creation and use of single method anonymous classes
instead of creating separate concrete class for functional interface
•They can accept zero or more parameters, any of which can be passed with or without type
specification.
•Then signature of lambda expression implementing Runnable interface will be () -> void.

TNS India Foundation | ‹#›


Partners in Economic Transformation
TNS India Foundation | ‹#›
Partners in Economic Transformation
Example

TNS India Foundation | ‹#›


Partners in Economic Transformation
Supplier

Supplier: Supplier represent an operation through which we can generate new values
in the stream. Some of the methods in Stream that takes Supplier argument are:

● public static<T> Stream<T> generate(Supplier<T> s)


● <R> R collect(Supplier<R> supplier,BiConsumer<R, ? super T>
accumulator,BiConsumer<R, R> combiner)

TNS India Foundation | ‹#›


Partners in Economic Transformation
Function

● A Function is another in-build functional interface in java.util.function package, the

function takes an input value and returns a value.

● The function interface has four methods, mostly function used in map feature of stream

APIs.
R apply(T var1);

default <V> Function<V, R> compose(Function<V, T> before);

default <V> Function<T, V> andThen(Function<R, V> after);


TNS India Foundation | ‹#›
Partners in Economic Transformation
Predicate

● A predicate is a Functional which accepts an argument and returns a boolean.

● Usually, it is used to apply in a filter for a collection of objects.


boolean test(T value);

default Predicate<T> and(Predicate<? super T> other);

default Predicate<T> negate();

default Predicate<T> or(Predicate<? super T> other);

TNS India Foundation | ‹#›


Partners in Economic Transformation
Consumer
● A Consumer is an in-build functional interface in the java.util.function package.

● we use consumers when we need to consume objects, the consumer takes an input

value and returns nothing.

● The consumer interface has two methods

void accept(T value);

default Consumer<T> andThen(Consumer<? super T> after);

TNS India Foundation | ‹#›


Partners in Economic Transformation
Uses of Lambda Expressions

● Lambda expressions are an important addition to Java that greatly improves overall
maintainability, readability, and developer productivity.
● They can be applied in many different contexts, ranging from simple anonymous
functions to sorting and filtering Collections.
● Lambda expressions can be assigned to variables and then passed into other
objects.
● It allows developers to write cleaner, shorter more readable, and reusable code.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. Which of the following annotations can be used to ensure that an interface is a
functional interface?

A @Functional

B @FunctionalInterface

C @FunctionalClass

D @FunctionalType

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. Which of the following annotations can be used to ensure that an interface is a
functional interface?

A @Functional

B @FunctionalInterface

C @FunctionalClass

D @FunctionalType

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2.In Java, which method is used to apply a lambda expression to each element of
a stream?

A forEach()

B map()

C filter()

D reduce()

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2.In Java, which method is used to apply a lambda expression to each element of
a stream?

A forEach()

B map()

C filter()

D reduce()

TNS India Foundation | ‹#›


Partners in Economic Transformation
Summary

● Java 8 Lambda expression (or function) is an anonymous function


● Removes the obvious code from implementation
● Syntax: (parameters) -> { statements; }
● Can have any number of parameters and any number of lines or expressions in body
● For one arguments parentheses are optional
● For single line of expression curly brackets and return keywords is optional
● Local variables used in lambdas expressions must be effective final variables.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question

Objective:
Create a simple task scheduler application using Java and lambda expressions. The
task scheduler should allow users to schedule tasks to be executed at specific intervals.

Requirements:
Task Interface:

Define a functional interface called Task with a single method execute() representing
the task to be performed.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question

Task Scheduler Class:

● Implement a class called TaskScheduler that will manage the scheduling of tasks.
● The TaskScheduler class should have a method scheduleTask(Task task, int
interval) to schedule a task to be executed at the specified interval (in seconds).
● Use lambda expressions to represent tasks when scheduling.

Testing:

● Write a program to demonstrate the usage of your TaskScheduler class.


● Schedule at least three different tasks with different intervals.
● Execute the tasks and observe the output.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Let’s Reflect -

1. What is your major learning from today’s session?


2. How are you going to use this learning in your journey on the job?

TNS India Foundation | ‹#›


Partners in Economic Transformation

You might also like