Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Let's start with the Predicate interface. It represents a single argument function that evaluates to a boolean. For instance, a Predicate can check if a number is greater than ten. Can anyone think of a scenario where we might use a Predicate?
We could use it for filtering elements in a list based on a condition!
Exactly! Predicates are perfect for filtering. Can you provide an example of how we would declare and use one?
Sure! We could write: Predicate<Integer> isPositive = x -> x > 0; and then use it in a filter operation.
Good job! Remember, the key function of a Predicate is to ensure that it returns true or false based on the input.
To remember this, think of the acronym 'PQ' for Predicate-Question: Predicate asks whether a statement is true or false.
Now, let's move on to the Function interface. This interface represents a function that takes one argument and produces a result. Can someone throw out what the syntax would look like?
I think it would be something like Function<String, Integer> stringLength = s -> s.length();
Exactly! And can someone tell me how we can use this Function in practice?
We could use it to convert a list of strings to their lengths, like: list.stream().map(stringLength).collect(Collectors.toList());
Correct! Functions are versatile and can be used in many operations. To help you remember, think of the acronym 'FR' for Function-Return: Function returns a result from its input.
Next up is the Consumer interface. Who can tell me what a Consumer does?
It takes an input and performs an operation without returning anything!
Exactly right! For example, using a Consumer, we can easily print each element of a list. Can someone show me how that's done?
Sure! We can write Consumer<String> printConsumer = System.out::println; and then use it in a forEach loop.
Perfect! And to remember this, think of the phrase 'Caring Consumer' - it cares for the input by performing an action!
Now, let’s talk about the Supplier interface, which provides a result without needing any input. Can someone provide an example of a Supplier?
An example could be a Supplier that supplies a random number. Like Supplier<Integer> randomNumber = () -> new Random().nextInt(100);
Great example! Suppliers are useful when you need to create or generate new values. To help memorize this, think of the phrase 'Supplying Success' – it supplies you with something without needing anything in return!
Finally, let's cover UnaryOperator and BinaryOperator. Can anyone explain their differences?
UnaryOperator works with a single type for both input and output, while BinaryOperator takes two arguments of the same type.
Correct! For instance, UnaryOperator<Integer> square = x -> x * x; vs BinaryOperator<Integer> add = (a, b) -> a + b;. To remember this difference, think of them as 'Single' for Unary and 'Double' for Binary!
That makes sense!
Excellent! Let's summarize what we’ve learned about the key interfaces today: Predicate checks conditions, Function transforms, Consumer performs actions, Supplier generates values, Unary operates on one, and Binary combines two.
A functional interface for evaluating boolean conditions.
An interface that maps one type to another type.
An interface for performing an action on a given input.
An interface for generating or supplying values.
A function that takes and returns the same type.
A function that combines two values of the same type.
Predicate Example: Predicate<Integer> isEven = x -> x % 2 == 0;
Function Example: Function<String, Integer> convertToLength = str -> str.length();
Consumer Example: Consumer<String> printMessage = msg -> System.out.println(msg);
Supplier Example: Supplier<Double> randomValue = () -> Math.random();
UnaryOperator Example: UnaryOperator<String> toUpperCase = str -> str.toUpperCase();
BinaryOperator Example: BinaryOperator<String> concatenate = (str1, str2) -> str1 + str2;
Predicate checks the truth, gives boolean proof.
Imagine a farmer who tests if his crop is ripe. He uses a Predicate to determine if it’s true or false based on color.
Remember 'FCP' for Function-Convert-Produce: A Function converts one output from another.
The acronym 'CASS' for Consumer-Action-No-Result, helps remember the essence of Consumer.
Term
What is a Predicate?
Click to flip
Term
What does a Consumer do?
Click to flip
Term
What is the purpose of a Supplier?
Click to flip
A functional interface representing a boolean-valued function of one argument.
A functional interface that takes one argument and returns a result.
A functional interface representing an operation that accepts a single input argument and returns no result.
A functional interface that supplies a result without taking any input.
A specialization of Function that takes a single argument and returns a result of the same type.
A specialization of BiFunction that takes two arguments of the same type and returns a result.
This section covers the built-in functional interfaces provided in Java's java.util.function package, exploring their purpose and usage through examples.
In this section, we discuss the various built-in functional interfaces available in the java.util.function package. These interfaces serve as foundational tools for functional programming in Java, including Predicates, Functions, Consumers, and more, providing developers with pre-defined templates for common functional operations.
The java.util.function package, introduced in Java 8, includes several useful predefined functional interfaces. These interfaces simplify the process of working with lambda expressions, making functional programming more accessible and efficient.
Predicate
Predicate<Integer> isGreaterThanTen = x -> x > 10;Function<T, R>: Accepts a parameter of type T and returns a result of type R. Example:
Function<String, Integer> stringLength = s -> s.length();Consumer
Consumer<String> printConsumer = System.out::println;Supplier
Supplier<String> greeting = () -> "Hello";UnaryOperator
UnaryOperator<Integer> doubleValue = x -> x * 2;BinaryOperator
BinaryOperator<Integer> add = (a, b) -> a + b;Understanding and utilizing these built-in functional interfaces promotes code reusability and clarity, leveraging the capabilities of functional programming in Java.