Understanding and testing of functional interface based on Java 8

1. Understanding of functional interfaces

According to the idea of refactoring, modules that are easy to change need to be abstracted and encapsulated. From this point of view, the functional interface newly introduced in Java 8 is designed based on this idea.

2. Functional interface definition

2.1 customization is as follows

The functional interface keyword is required to display the Declaration:

2.2 system predefined

You can check the source code to understand the specific details. These interfaces include some common scenarios, which can generally meet the needs

3. Use of functional interface

Functional interfaces generally need to be defined before use, or several predefined functional interfaces can be used

There is no difference between using a functional interface and using a variable. The declaration definition is displayed in the following format:

Although the interface here looks like a variable, it is actually a section of behavior code, which is used to execute specific business logic. It can be freely passed between method interfaces or directly executed

interface. doSomeThing();

For example, define a functional interface as an interface with parameters:

4. Functional interface exercise

4.1 custom entity class

4.2 user defined functional interface

The interface has a test method that does not receive any parameters and does not return any data

4.3 testing user defined functional interfaces

So far, we have completed the definition and call of a very simple functional interface

4.4 system predefined functional interface

Consumer < T >: the interface receives an object T and returns void. The test is as follows:

Supplier < T >: the interface does not receive any parameters and returns an object T. the test is as follows:

Predict < T >: this interface receives an object T and returns a Boolean

Function < T, R >: the interface receives an object T and returns an object R after conversion judgment

The above article on understanding and testing functional interfaces based on Java 8 is all that Xiaobian has shared with you. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>