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

Slides Lambda Expressions Functional Interfaces and Method References Java's Functional Interfaces Consumer & Predicate

Uploaded by

quý nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Slides Lambda Expressions Functional Interfaces and Method References Java's Functional Interfaces Consumer & Predicate

Uploaded by

quý nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

java.util.

function

Java provides a library of functional interfaces in the java.util.function package.


We looked at one already, the Consumer interface.
I'll look at another of these interfaces now, the BinaryOperator, in code.

COMPLETE JAVA MASTERCLASS


Java's Functional Interfaces, Consumer & Predicate
The Four basic categories of Functional Interfaces in java.util.function package
It's a good idea to know the four basic types of functional interfaces in the java.util.function
package.
There are over forty interfaces in this package.
These can all be categorized as one of the following types.
This slide shows the four categories, with the simplest method shown.

Interface Category Basic Method Signature Purpose


Consumer execute code without returning data

Function return a result of an operation or function

Predicate test if a condition is true or false

Supplier return an instance of something

COMPLETE JAVA MASTERCLASS


Java's Functional Interfaces, Consumer & Predicate
The Consumer interface

On this slide, I'm showing the two most common Consumer interfaces, and the functional
method on each.
The Consumer interface takes one argument of any type.
The BiConsumer interface takes two arguments, of two different types.
Interface Name Method Signature
Consumer

BiConsumer

COMPLETE JAVA MASTERCLASS


Java's Functional Interfaces, Consumer & Predicate
A Consumer Lambda Expression Example

This slide shows an example consumer lambda expression. It takes one argument and
executes a single statement.
No result is returned.

Example Lambda Expression for Consumer Consumer Method

COMPLETE JAVA MASTERCLASS


Java's Functional Interfaces, Consumer & Predicate
The Predicate Interface

The predicate interfaces take one or two arguments, and always returns a boolean value.
They are used to test a condition, and if the condition is true, some operation will be
performed.

Interface Name Method Signature


Predicate

BiPredicate

COMPLETE JAVA MASTERCLASS


Java's Functional Interfaces, Consumer & Predicate
A Predicate Lambda Expression Example

In this example, the expression takes a String, and tests if it's equal to the literal text, “Hello”
here, ignoring case, so it returns either true or false.

Example Lambda Expression for Consumer

COMPLETE JAVA MASTERCLASS


Java's Functional Interfaces, Consumer & Predicate

You might also like