Source: juejin.cn/post /7011435192803917831
Welcome to join Xiaoha’s Planet, you will get: Exclusive project practice/Java learning route/One-on-one questions/Learning check-in/Gift book benefits
Currently, I am leading my friends to work on the first project within the planet: Full StackSeparation of front-end and back-end blog, hands-on, back-end + front-end full-stack development, from 0 to 1 Explain the development steps of each function point and answer questions 1v1 until the project goes online. At present, 125 sections have been updated, with a total of 190,000+ words, and 805 explanation pictures. It is still being updated. More projects will be added in the future. The goal is to complete a wave of typical projects in the Java field, such as flash sales. System, online mall, IM instant messaging, Spring Cloud Alibaba, etc., click me to join the learning, 410+ friends have joined (early bird price is super low)
-
Function
Functional interface-
Supplier
Supply function -
Consumer
Consumption function -
Runnable
No parameters and no return function -
Function
The expression of a function is to receive a parameter and return a value.Supplier
,Consumer
andRunnable
can be regarded as a special representation ofFunction
-
-
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
Java 8 >Interface 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
No parameters and no return function
The expression form of Runnable
is that it has no parameters and no return value.
Function
The expression of a function is to receive a parameter and return a value. Supplier
, Consumer
and Runnable
can be regarded as a special expression of Function
Tips
Handling if exceptions thrown
-
define function
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); }
-
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); } }; }
-
Usage
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
-
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); }
-
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(); } }; }
-
Usage
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
-
define function
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); }
-
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); } }; }
-
Usage
After calling the tool class parameters, call the presentOrElseHandle
method of Functional Interface
and pass in a Consumer
and Runnable
When the parameter is not empty, print the parameter
When the parameter is not empty
Welcome to join Xiaoha’s Planet, you will get: Exclusive project practice/Java learning route/One-on-one questions/Learning check-in/Gift book benefits
Currently, I am leading my friends to work on the first project within the planet: Full StackSeparation of front-end and back-end blog, hands-on, back-end + front-end full-stack development, from 0 to 1 Explain the development steps of each function point and answer questions 1v1 until the project goes online. At present, 125 sections have been updated, with a total of 190,000+ words, and 805 explanation pictures. It is still being updated. More projects will be added in the future. The goal is to include all typical projects in the Java field, such as flash sales. System, online mall, IM instant messaging, Spring Cloud Alibaba, etc., click me to join the learning, 410+ friends have joined (early bird price is super low)
1. My private study circle~
2. To understand how to live in a different place, just read this article
3. The comparison between Chinese and American programmers is too real. . .
4. Top tool, a powerful Java entity conversion/copying tool
I recently interviewed BAT and compiled an interview material "Java Interview BATJ Clearance Manual", which covers Java core technology, JVM, Java concurrency, SSM, microservices, databases, data structures, etc. How to get it: Click "Watching", follow the official account and reply to Java to get it. More content will be provided one after another.
PS: Because the official account platform has changed the push rules, if you don’t want to miss the content, remember to click “Reading” after reading and add a “star”, so that each new article will be pushed to you as soon as possible. in the subscription list. Click "Watching" to support Xiaoha, thank you