• notice
  • Congratulations on the launch of the Sought Tech site

This interface in Java 8 is awesome

---

In the development program, if...else... is often used to judge and throw exceptions, branch processing and other operations.These if...else... code> is flooded in the code, which seriously affects the beauty of the code.At this time, we can use the Function interface of Java 8 to eliminate if...else...,

if (...){
    throw new RuntimeException("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 The interface is mainly divided into Supplier supply function, Consumer consumption function, Runnable no parameter and no return function and Function have return function,

Function can be seen as a conversion function

Supplier supply function

Supplier does not accept arguments and only returns data

Consumer consumer function

Consumer is the opposite of Supplier, Consumer takes an argument and returns no value

Runnable no parameter no return function

The expression of

Runnable is that there is no argument and no return value.Search the public vertical number: MarkerHub, follow the reply [ vue ] to get the front-end and back-end introductory tutorial!

Function functions take an argument and return a value, Supplier , Consumer and Runnable can be seen as a special form of Function

Use tips

Handle the if

that throws an exception
  1. Define function

Define a functional interface in the form of throwing exceptions, this interface only has arguments and no return value is a consumer interface

/**
 * Throwing exception interface
 **/
@FunctionalInterface
public interface ThrowExceptionFunction {

   /**
     * throw exception information
     *
     * @param message exception information
     * @return void
     **/
    void throwMessage(String message);
}

  1. Writing judgment method

Create a tool class VUtils and create a isTure method, the return value of the method is the functional interface-ThrowExceptionFunction , the interface implementation logic of ThrowExceptionFunction is to throw an exception when the argument b is true

/**
 * if argument is true throw exception
 *
 * @param b
 * @return com.example.demo.func.ThrowExceptionFunction
 **/
public static ThrowExceptionFunction isTure(boolean b){

    return (errorMessage)-> {
        if (b){
            throw new RuntimeException(errorMessage);
        }
    };
}

  1. How to use

After calling the parameter of the tool class, call the throwMessage method of the functional interface to pass in the exception information, when the parameter is false executes normally

Throw exception when incoming and outgoing arguments are true

Handling if branch operations

  1. Define a functional interface

Create a functional interface named BranchHandle, the interface argument is two Runnable interfaces, the two Runnable The interface represents what to do when true or false respectively

/**
 * Branch handling interface
 **/
@FunctionalInterface
public interface BranchHandle {

   /**
     * Branch operation
     *
     * The operation to be performed when @param trueHandle is true
     * @param falseHandle what to do when false
     * @return void
     **/
    void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);

}

  1. Writing judgment method

Create a method named isTureOrFalse, the return value of the method is the function interface-BranchHandle, just defined.p>

/**
 * When the argument is true or false, perform different operations 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();
        }
    };
}

  1. How to use

When the argument is true, execute trueHandle

When the argument is false, execute falseHandle

If there is a value, perform a consuming operation, otherwise perform a null-based operation

  1. Define function

Create a functional interface named PresentOrElseHandler, the parameters of the interface are a Consumer interface and a Runnable, respectively Represents the consumption operation performed when the value is not empty and other operations performed when the value is empty

/**
 * Null and non-null branch handling
 */
public interface PresentOrElseHandler<T extends Object> {

   /**
     * Execute the consumption operation when the value is not empty
     * perform other operations when the value is empty
     *
     * When @param action value is not empty, the consumption action to execute
     * @param emptyAction The action to be performed when the value is empty
     * @return void
     **/
   void presentOrElseHandle(Consumer<? super T> action, Runnable emptyAction);
   
}

  1. Writing judgment method

Create a method named isBlankOrNoBlank, the return value of the method is the Functional Interface-PresentOrElseHandler , just defined p>

/**
 * When the argument is true or false, perform different operations 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);
        }
    };
}

  1. How to use

After calling the tool class argument, call the presentOrElseHandle method of the functional interface and pass in a Consumer and Runnable

If the argument is not empty, print the argument

When the argument is not empty

End

Function The functional interface is a very important feature of java 8, making good use of the Function function can greatly simplify the code,

Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+