函数式接口Function(一)


Function 函数式接口初识


 

使用注解@FunctionalInterface标识,并且只包含一个抽象方法的接口是函数式接口。函数式接口主要分为Supplier供给型函数、Consumer消费型函数、Runnable无参无返回型函数和Function有参有返回型函数。

1.代码示例

@FunctionalInterface
public interface BranchHandle {


    void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);


}
public class VUtils {



    public static ThrowExceptionFunction isTure(boolean b){

        return (errorMessage) ->{

            if(b){
                throw new RuntimeException(errorMessage);
            }

        };

    }


    public static BranchHandle isTureOrFalse(boolean b){

        return (trueHandle, falseHandle) -> {
            if (b){
                trueHandle.run();
            } else {
                falseHandle.run();
            }
        };
    }

}
  VUtils.isTureOrFalse(false).trueOrFalseHandle(() -> {
            System.out.println("true handler ....");
        }, () -> {
            System.out.println("false handler ....");
        });