As in previous topic, we can use functional interfaces to use in lambda expression see topic on: Java 8 interfaces
However Java 8 goes on more to provide us mechanism to easier create lambda expressions. See java.util.function library. No need to create an functional interface and just overwrite existing ones in this library.
This library can be divided in to four sections:
Consumers are interfaces that focuses on accepting parameters. for example: BiConsumer – as the api reads, this accepts two arguments and produces no result:
Functions are interfaces that are focused on doing accepting a parameter and returning a result. for example: BiFunction – as the api reads, accepts two arguments and returns one result we can use it as below:
In contrast, Supplier methods just returns an object without an parameters:
Finally we have UnaryOperator methods in the library, it is much similar to consumer, but in a way it just accepts an object and returns that same type.
The best way to use UnaryOperators are for when using objects you have created, see example below.
In this case I have created a People class, and the output will also be the same class: