Article directory
- Function functional interface
-
- Supplier supply function
- Consumer consumption function
- Runnable no parameter no return function
- The Function function takes a parameter and returns a value. Supplier, Consumer and Runnable can be regarded as a special form of Function
- Use tips
-
- Handle if that throws an exception
- Handle if branch operations
- If a value exists, perform a consumption operation, otherwise perform a null-based operation.
In the development process, if...else...
is often used to judge and throw exceptions, branch processing and other operations. These if...else...
are flooded in the code and seriously affect the appearance of the code. At this time, we can use the Function interface of Java 8
to eliminate if...else...
.
if (...){<!-- --> throw new RuntimeException("An exception occurred"); } if (...){<!-- --> doSomething(); } else {<!-- --> doOther(); }
Function functional interface
An interface marked with the annotation @FunctionalInterface
and containing only one abstract method
is a functional interface
. Functional interface
is mainly divided into Supplier
supply function, Consumer
consumption function, Runnable
no parameter and no return type Functions and Function
have parameters and return functions.
Function can be regarded as a conversion function
Supplier supply function
The expression form of Supplier
is that it does not accept parameters and only returns data.
Consumer consumption function
Consumer
consumer functions are just the opposite of Supplier
. Consumer
receives one parameter and has no return value
Runnable function with no parameters and no return
The expression form of Runnable
is that it has no parameters and no return value.
Function function takes a parameter and returns a value. Supplier, Consumer and Runnable can be regarded as a special form of Function
Usage Tips
Handling if exceptions thrown
1. Define functions
Define a functional interface
in the form of throwing an exception. This interface has only parameters and no return value. It is a consumer interface
/** *Exception throwing interface **/ @FunctionalInterface public interface ThrowExceptionFunction {<!-- --> /** * Throw exception information * * @param message exception information * @return void **/ void throwMessage(String message); }
2. Write a judgment method
Create a tool class VUtils
and create a isTure
method. The return value of the method is the Functional Interface
–ThrowExceptionFunction
just defined. >. The interface implementation logic of ThrowExceptionFunction
is to throw an exception when the parameter b
is true
/** * Throws an exception if the parameter is true * * @param b * @return com.example.demo.func.ThrowExceptionFunction **/ public static ThrowExceptionFunction isTure(boolean b){<!-- --> return (errorMessage) -> {<!-- --> if (b){<!-- --> throw new RuntimeException(errorMessage); } }; }
3. How to use
After calling the tool class parameters, call the throwMessage
method of Functional Interface
to pass in the exception information. Normal execution when the incoming and outgoing parameters are false
An exception is thrown when the incoming and outgoing parameters are true
Processing if branch operations
1. Define functional interface
Create a functional interface named BranchHandle
. The parameters of the interface are two Runnable
interfaces. These two Runnable
interfaces respectively represent the operations to be performed when it is true
or false
/** * Branch processing interface **/ @FunctionalInterface public interface BranchHandle {<!-- --> /** * Branch operations * * @param trueHandle The operation to be performed when it is true * @param falseHandle The operation to be performed when it is false * @return void **/ void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle); }
2. Write a judgment method
Create a method named isTureOrFalse
. The return value of the method is the Functional Interface
–BranchHandle
just defined.
/** * When the parameter is true or false, different operations are performed respectively. * * @param b * @return com.example.demo.func.BranchHandle **/ public static BranchHandle isTureOrFalse(boolean b){<!-- --> return (trueHandle, falseHandle) -> {<!-- --> if (b){<!-- --> trueHandle.run(); } else {<!-- --> falseHandle.run(); } }; }
3. How to use
When the parameter is true
, execute trueHandle
When the parameter is false
, execute falseHandle
If the value exists, perform the consumption operation, otherwise perform the null-based operation
1. Define functions
Create a functional interface named PresentOrElseHandler
. One parameter of the interface is the Consumer
interface. One is Runnable
, which respectively represent consumption operations performed when the value is not empty and other operations performed when the value is empty.
/** * Null value and non-null value branch processing */ public interface PresentOrElseHandler<T extends Object> {<!-- --> /** * Execute consumption operation when the value is not empty * Perform other operations when the value is empty * * @param action The consumption operation performed when the value is not empty * @param emptyAction The action to be performed when the value is empty * @return void **/ void presentOrElseHandle(Consumer<? super T> action, Runnable emptyAction); }
2. Write a judgment method
Create a method named isBlankOrNoBlank
. The return value of the method is the Functional Interface
–PresentOrElseHandler
just defined.
/** * When the parameter is true or false, different operations are performed respectively. * * @param b * @return com.example.demo.func.BranchHandle **/ public static PresentOrElseHandler<?> isBlankOrNoBlank(String str){<!-- --> return (consumer, runnable) -> {<!-- --> if (str == null || str.length() == 0){<!-- --> runnable.run(); } else {<!-- --> consumer.accept(str); } }; }
3. How to use
After calling the tool class parameters, call the presentOrElseHandle
method of the functional interface and pass in a Consumer
and Runnable
When the parameter is not empty, print the parameter
When the parameter is not empty