Detailed explanation of built-in functional interface of new features of Java 8
Java 8 new features built-in functional interface
In a previous blog post lambda expression, we mentioned the functional interface provided by Java 8. In this article, we will introduce the four most basic functional interfaces of Java 8
For method references, strictly speaking, an interface needs to be defined. No matter how we operate, there are only four possible interfaces.
Java 8 provides a functional interface package Java util. function.*, There are many functional interfaces built in Java 8 under this package. However, it is basically divided into four basic types:
Functional interface
With t as input and R as output, it also contains default methods combined with other functions.
Sample code
Consumer interface
Take t as input and return nothing, indicating the operation on a single parameter.
Sample code
Supply interface (Supplier)
There are no input parameters, only t returns the output
Sample code
Predicate interface
Taking t as input and returning a Boolean value as output, the interface contains a variety of default methods to combine predicate into other complex logic (and, or, non).
Sample code
Therefore, in Java 8, because there are more than four functional interfaces, it is generally rare for users to define new functional interfaces!
Thank you for reading, hope to help you, thank you for your support to this site!