The Function<T,R> interface of Java functional programming introduction

Original article by Liu Dun on 2022-09-24

Let me raise a question first:
Can a function be passed directly as a parameter in Java like JavaScript?
As of Java 8, it is possible. In the functional programming interface provided by Java, the Function<T,R> interface is used to represent a function (method) whose parameter type is T and return value type is R. Next, take the encryption function as an example to realize the effect of passing a function as a parameter in Java.

1. Declaration method
First, let's declare a text encryption method whose parameter type is function. This method receives the input string from the console, calls the incoming encryption function to encrypt, and returns.
注意这个方法的参数,是Function<String,String>,说明可以传入的参数是一个函数,函数的参数是String,返回值也是String类型。

/**
 * 对控制台文本进行加密的方法
 * @param signer 传入的加密函数
 * @return 加密之后的密文
 */
protected static String sign(Function<String, String> signer) {
    
    
    System.out.println("请输入要加密的字串:");
    String str = new Scanner(System.in).next();
    return signer.apply(str);
}

2. Call the method and pass in a function (method 1)
declare an encryption function md5Sign based on the MD5 algorithm, call the sign method in the main method and pass in the encryption function.

/**
 * md5加密函数
 * @param signStr 要加密的明文
 * @return 加密后的密文
 */
protected static String md5Sign(String signStr) {
    
    
    MessageDigest md5 = null;
    try {
    
    
        md5 = MessageDigest.getInstance("md5");
    } catch (NoSuchAlgorithmException e) {
    
    
        e.printStackTrace();
    }
    return new String(md5.digest(signStr.getBytes()));
}

public static void main(String[] args)throws Exception {
    
    
    String result = sign(FunctionTest::md5Sign);
    System.out.println("加密后:"+result);
}

3. Call the method and pass in a function (mode 2)
The above method declares the function to be passed in separately, but if there are not many codes, you can omit the declaration and pass in directly when calling. This syntax Called Lambda expression. Lambda expression can also be regarded as a kind of anonymous function.
Lambda expression is divided into three parts: (formal parameter) -> {function body code}, the whole expression is actually a function. The first part, if there is only one formal parameter, () can be omitted; the second part is an arrow, which is fixed to be written; the third part is the code, if the code has only one line, {} can be omitted.

public static void main(String[] args){
    
    
    String result = sign(str -> {
    
     //Lambda表达式从从str -> 这里开始 
        try {
    
    
            return new String(MessageDigest.getInstance("md5").digest(str.getBytes()));
        } catch (NoSuchAlgorithmException e) {
    
    
            e.printStackTrace();
        }
        return null;
    }); //Lambda表达式在}这里结束
    System.out.println("加密后:"+result);
}

Guess you like

Origin blog.csdn.net/liudun_cool/article/details/127028259