Function
The Function interface can encapsulate an operation, and allows chaining functions by using the compose and andThen methods.
Chaining Functions
The interface contains default methods to chain operations through function composition:
Mapping Methods
Any method can be used as a function.
In this case an abstract method will be used as a strategy, to support the template method pattern. This method is as follows:
Then we have another method ready to make use of a function with the same erasure:
And now this can be used like this:
While at first this may be overly complex, take not that in a real application the second method would encapsulate complex operations, allowing the programmer to just change the particulars of the read operation:
Extensions
The JDK includes a BiFunction, which supports two arguments as input, and a few extensions for numeric primitives, and a binary operator which takes two instances of the same type.
Last updated
Was this helpful?