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.
Today, we're exploring the built-in functional interfaces found in the java.util.function package. These interfaces allow us to write cleaner and more efficient code using lambda expressions.
What exactly is a functional interface?
Great question! A functional interface is an interface that contains exactly one abstract method. This allows us to use lambda expressions to implement that method.
Can you give us an example?
Sure! Think of Predicate<T>: it takes one argument and returns a boolean. An example is x -> x > 10, which checks if a number is greater than 10.
Let's go through each functional interface. Starting with Function<T,R>, it maps a value of type T to a value of type R.
How would you use that in code?
An example of that would be s -> s.length(), which takes a string and returns its length.
What about Consumer<T>?
Good point! Consumer<T> takes in a value and doesn’t return anything. For instance, s -> System.out.println(s) prints the string.
Now, we have Supplier<T>, which provides a value without taking any arguments. For example, () -> new Random().nextInt().
That sounds handy for generating random numbers!
Absolutely! Then there's UnaryOperator<T>, which takes one argument of type T and returns a value of the same type. For instance, x -> x * x.
And what about BinaryOperator<T>?
Exactly! It takes two arguments of type T and returns a single result. An example is (x, y) -> x + y, which sums two numbers.
Lastly, let’s talk about how we can use these interfaces effectively. We can pass these functional interfaces as arguments to methods.
Like in the Streams API?
Precisely! The Streams API heavily utilizes functional interfaces, allowing operations like filtering and mapping with ease.
So Predicate can filter elements of a stream?
Yes! And Function can transform the data as needed, making our code declarative and clear.
Java 8 provides several functional interfaces that facilitate functional programming.
Predicate
Function<T,R>: Converts one input of type T into an output of type R.
Consumer
Supplier
UnaryOperator
BinaryOperator
Predicate Example: Predicate<Integer> isGreaterThan10 = x -> x > 10;
Function Example: Function<String, Integer> stringLength = s -> s.length();
Consumer Example: Consumer<String> printString = s -> System.out.println(s);
Supplier Example: Supplier<Double> randomValue = () -> Math.random();
UnaryOperator Example: UnaryOperator<Integer> square = x -> x * x;
BinaryOperator Example: BinaryOperator<Integer> add = (x, y) -> x + y;
A Predicate checks, it's true or false, / While Supplier brings values, with no need to toss.
Imagine a shopkeeper (Supplier) that gives you fruits without asking what you want, while a judge (Predicate) decides if you can take a fruit based on its ripeness.
Fabulous People Can Supply Unique Beverages: Predicate, Function, Consumer, Supplier, Unary Operator, Binary Operator.
P-F-C-S-U-B: Predicate, Function, Consumer, Supplier, Unary Operator, Binary Operator.
Term
What does Predicate<T> do?
Click to flip
Term
Define Function<T,R>.
Click to flip
Term
What is a Consumer<T>?
Click to flip
Term
What does Supplier<T> do?
Click to flip
Term
What is UnaryOperator<T>?
Click to flip
Term
Define BinaryOperator<T>.
Click to flip
A functional interface that represents a function that takes one argument and returns a boolean value.
A functional interface that accepts one argument of type T and produces a result of type R.
A functional interface that takes a single argument and returns no result.
A functional interface that does not take any input but provides a result of type T.
A functional interface that takes one argument of type T and returns a value of type T.
A functional interface that takes two arguments of the same type and returns a result of the same type.
Java 8 introduced built-in functional interfaces that simplify functional programming.
This section discusses the pre-defined functional interfaces available in the java.util.function package, each serving a distinct purpose. It includes examples of lambda expressions that correspond to each interface, making it easier for developers to use them efficiently in Java applications.
In Java 8, the java.util.function package was introduced, encompassing a variety of built-in functional interfaces that are vital for functional programming. These interfaces enable developers to pass behaviors, simplifying code and enhancing readability. The primary built-in functional interfaces include:
x -> x > 10s -> s.length()s -> System.out.println(s)() -> new Random().nextInt()x -> x * x(x, y) -> x + yEach of these interfaces plays a crucial role in writing concise and clear functional code, allowing developers to leverage lambda expressions efficiently.