Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Function interface in Java8 to destroy if...else

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use the Function interface in Java8 to destroy if...else. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

If...else... is often used in the development process Perform operations such as judging and throwing exceptions, branch processing and so on. These if...else... Flooding the code seriously affects the beauty of the code, so we can use the Function interface of Java 8 to destroy if...else....

If (...) {throw new RuntimeException ("exception occurred");} if (...) {doSomething ();} else {doOther ();} Function functional interface

Interfaces that are identified with the annotation @ FunctionalInterface and contain only one abstract method are functional interfaces. The functional interface is mainly divided into Supplier supply function, Consumer consumption function, Runnable no parameter no return function and Function parameter return function.

Function can be regarded as a transformational function.

Supplier supply function

Supplier is represented by not accepting parameters and only returning data.

Consumer consumption function

The consumption function of Consumer is the opposite of Supplier. Consumer receives a parameter and no value is returned

Runnable function with no parameter and no return

Runnable is expressed in the form of no parameters and no return value.

The Function function takes a parameter and returns a value. Supplier, Consumer and Runnable can be regarded as a special form of Function.

Use tricks to handle if that throws an exception

1. Define function

Defines a functional interface in the form of throwing an exception, which has only parameters and no return value is a consumer interface

/ * API for throwing exceptions * * / @ FunctionalInterfacepublic interface ThrowExceptionFunction {/ * throwing exception information * * @ param message exception information * @ return void * * / void throwMessage (String message);}

two。 Write a judgment method

Create the utility class VUtils and create an isTure method with the return value of the functional interface just defined-ThrowExceptionFunction. The interface implementation logic of ThrowExceptionFunction is to throw an exception when parameter b is true

/ * if the parameter is true, throw an exception * * @ param b * @ return com.example.demo.func.ThrowExceptionFunction * * / public static ThrowExceptionFunction isTure (boolean b) {return (errorMessage)-> {if (b) {throw new RuntimeException (errorMessage);}};}

3. Mode of use

After calling the tool class parameter parameters, the exception information is passed in by calling the throwMessage method of the functional interface. Execute normally when the parameter of entry and exit is false

Throw an exception when the input and exit parameter is true

Handle if branch operations

1. Define functional interface

Create a functional interface named BranchHandle with two Runnable interfaces as parameters. These two Runnable interfaces represent the actions to be performed when it is true or false, respectively.

/ * Branch processing interface * * / @ FunctionalInterfacepublic interface BranchHandle {/ * * Branch operation * * @ what to do if param trueHandle is true * @ what to do if param falseHandle is false * @ return void * * / void trueOrFalseHandle (Runnable trueHandle, Runnable falseHandle);}

two。 Write a judgment method

Create a method named isTureOrFalse, and the return value of the method is the functional interface just defined-BranchHandle.

Perform different operations when / * parameter is true or false, * * @ 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. Mode of use

Execute trueHandle when the parameter is true

Execute falseHandle when the parameter is false

If there is a value to perform the consumption operation, otherwise the null-based operation is performed

1. Define function

Create a functional interface named PresentOrElseHandler, with one of the parameters of the interface Consumer interface. One is Runnable, which represents the consumption operation when the value is not empty and other operations when the value is empty.

/ * Null and non-null branch processing * / public interface PresentOrElseHandler {/ * perform consumption operation when the value is not null * perform other actions when the value is empty * * @ param action value is not empty, perform consumption operation * @ param emptyAction value is null, the action performed is * @ return void * * / void presentOrElseHandle (Consumer

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report