Chapter 3. Functional Interfaces of the JDK
Many functional programming languages use only a singular and dynamic concept of “functions” to describe their lambdas, regardless of their arguments, return type, or actual use case.
Java, on the other hand, is a strictly typed language requiring tangible types for everything, including lambdas.
That’s why the JDK provides you with over 40 readily available functional interfaces in its java.util.function
package to kick-start your functional toolset.
This chapter will show you the most important functional interfaces, explain why there are so many variations, and show how you can extend your own code to be more functional.
The Big Four Functional Interface Categories
The 40+ functional interfaces in java.util.function
fall into four main categories, with each category representing an essential functional use case:
-
Functions accept arguments and return a result.
-
Consumers only accept arguments but do not return a result.
-
Suppliers do not accept arguments and only return a result.
-
Predicates accept arguments to test against an expression and return a
boolean
primitive as their result.
These four categories cover many use cases, and their names relate to functional interface types and their variants.
Let’s take a look at the four main categories of functional interfaces.
Functions
Functions, with their corresponding java.util.function.Function<T, R>
interface, are one of the most central functional interfaces. They represent ...
Get A Functional Approach to Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.