Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Lambda

The document discusses functional interfaces and lambda expressions in Java. Functional interfaces have a single abstract method and can be used as function types. Lambda expressions are anonymous functions that can implement functional interfaces. Built-in functional interfaces like Predicate, Consumer, and Function are provided to represent common function types.

Uploaded by

yunus emre bey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lambda

The document discusses functional interfaces and lambda expressions in Java. Functional interfaces have a single abstract method and can be used as function types. Lambda expressions are anonymous functions that can implement functional interfaces. Built-in functional interfaces like Predicate, Consumer, and Function are provided to represent common function types.

Uploaded by

yunus emre bey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Functional Interface & Lambda Expression

Functional Interface (SAM)

• Known as SAM (Single Abstract Method)


interface

• There is only one abstract method in the


interface

• Effectively acts as a function

• @FunctionalInterface annotation is
applicable (Optional)
Functional Interface

Define functional interface

Abstract method
Lambda Expression

• A function with no name and an identifier

• Can be defined in the place where they are needed

• Expresses the instances of a functional Interface

• Can be assigned to the instance of functional


interface
Syntax of Lambda Expression

(int agr1, double arg2… ) -> { Statements }

Parameters Arrow Lambda expression’s


token body

Syntax Description
Takes no argument and executes the given statement(s) in
( ) -> { statements }
lambda expression’s body
Takes argument(s) and executes the given statements in
(Parameters) -> { statements }
lambda expression’s body
Custom Functional interface Example

Abstract method

Lambda Expression:
Implementation of functional
interface’ abstract method
Build In Functional Interfaces:
• Predicate
• Consumer
• Function
• BiPredicate
• BiConsumer
• BiFunction
Build in Functional Interface: Predicate

• Represents a function which takes one argument (any object) and returns boolean

Abstract method
Predicate Interface Example

Lambda Expression: Implementation


of predicate interface’s abstract
method

Lambda Expression: Implementation


of predicate interface’s abstract
method
Build in Functional Interface: Consumer

• Represents a function which takes one argument (any object) and does not return a value

Abstract method
Consumer Interface Example

Lambda Expression:
Implementation of Consumer
interface’s abstract method
Build in Functional Interface: Function

• Represents a function which takes one argument (any object) and return a value (any object)

Abstract method
Function Interface Example

Lambda Expression:
Implementation of function
interface’s abstract method
Build in Functional Interface: BiPredicate

• Represents a function which takes two arguments (any objects) and returns boolean

Abstract method
BiPredicate Interface Example

Lambda Expression:
Implementation of BiPredicate
interface’s abstract method
Build in Functional Interface: BiConsumer

• Represents a function which takes two argument (any object) and does not return a value

Abstract method
BiConsumer Interface Example

Lambda Expression:
Implementation of
BiConsumer interface’s
abstract method
Build in Functional Interface: BiFunction

• Represents a function which takes two arguments (any objects) and return a value (any object)

Abstract method
BiFunction Interface Example

Lambda Expression:
Implementation of
BiPredicate interface’s
abstract method
Stream
Stream

• Stream is not a data structure

• Stream is a method that takes inputs from a data structure ( Array & Collection )

• Stream is unable to change the data structure


Methods of Stream

• After calling the stream() function from an Array/Collection, we can access to the
methods of stream

Method Method Methods

distinct() collect() toArray()

skip() limit() map()

filter() count() forEach()

allMatch() anyMatch() nonMatch()

You might also like