Commonly used functional interface-Function interface

Function interface: The
java.util.function.Function<T,R> interface is used to obtain data of one type according to the data of another type. The
former is called a precondition and the latter is called a postcondition.

Abstract method: apply: The
main abstract method in the Function interface is: R apply(T t), which obtains the result of type R according to the parameters of type T.
The usage scenario is for example: converting the String type to the Integer type.

Use example:

public class Demo01Function {
    
    
    /*
        定义一个方法
        方法的参数传递一个字符串类型的整数
        方法的参数传递一个Function接口,泛型使用<String,Integer>
        使用Function接口中的方法apply,把字符串类型的整数,转换为Integer类型的整数
    */
    public static void change(String s, Function<String, Integer> fun) {
    
    
        //Integer in = fun.apply(s);
        int in = fun.apply(s);//拆箱装箱
        System.out.println(in);
    }

    public static void main(String[] args) {
    
    
        //定义一个String类型的整数
        String a = "1234";
        //调用change方法,将String类型的整数转换为Integer类型的整数,使用Lambda接口
       /* change(a, (String str) -> {
            return Integer.parseInt(str);
        });*/

        //优化Lambda
        change(a, str ->Integer.parseInt(str));
    }
}

Program demonstration:
Insert picture description here
Default method: andThen:
There is a default andThen method in the Function interface for combined operations. JDK source code such as:

default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
    
    
	Objects.requireNonNull(after);
	return (T t)> after.apply(apply(t));
}

This method is also used in the "do what first, then do what" scenario, similar to andThen in Consumer:

Usage example:
Requirements:
Convert the "123" of the String type to the Integer type, add 10
to the converted result , and convert the data of the Integer type after the increase to the String type

Analysis: The first
conversion
is to convert the String type to the Integer type,
so we can use Function<String,Integer> fun1
Integer i = fun1.apply("123")+10; the
second time is to convert the Integer type For the String type,
we can use Function<Integer,String> fun2
String s = fun2.apply(i);
We can use the andThen method to combine the two conversions using
String s = fun1.andThen(fun2).apply( "123");
fun1 first calls the apply method to convert the string to Integer
fun2 then calls the apply method to convert Integer to the string

public class Demo02Function_andThen {
    
    
    /*
        定义一个方法
        参数传递一个字符串类型的整数
        参数再传递两个Function接口
            一个泛型使用Function<String,Integer>
            一个泛型使用Function<Integer,String>
    */
    public static void change(String a, Function<String, Integer> fun1, Function<Integer, String> fun2) {
    
    
        String s = fun1.andThen(fun2).apply(a);
        System.out.println(s);
    }

    public static void main(String[] args) {
    
    
        //定义一个字符串类型的整数
        String s = "123";
        //调用change方法,传递字符串和两个Lambda表达式
        /*change(s, (String str) -> {
            return Integer.parseInt(str) + 10;
        }, (Integer i) -> {
            //把整数转换为字符串
            return i + "";
        });*/

        //Lambda优化
        change(s, str -> Integer.parseInt(str) + 10,  i ->  i + "");
    }
}

Program demonstration:
Insert picture description here
Exercise: Custom function model stitching.
Question
Please use Function to stitch function models. The multiple function operations that need to be executed in order are
String str = "Liu Yifei, 32";

Analysis:
1. Intercept the numeric age part of the string to obtain a string;
Function<String,String> "Liu Yifei,32" -> "32"
2. Convert the string in the previous step to an int type number;
Function<String ,Integer> "32"->32
3. Accumulate the int number from the previous step by 100, and get the result int number
Function<Integer,Integer> 32->132

Code:

public class Demo03Test {
    
    
    /*
        定义一个方法
        参数传递包含姓名和年龄的字符串
        参数再传递3个Function接口用于类型转换
    */
    public static int change(String s, Function<String,String> fun1,
                             Function<String,Integer> fun2,Function<Integer,Integer> fun3){
    
    
        //使用andThen方法把三个转换组合到一起
        return fun1.andThen(fun2).andThen(fun3).apply(s);
    }

    public static void main(String[] args) {
    
    
        //定义一个字符串
        String str = "刘亦菲,32";
        //调用change方法,参数传递字符串和3个Lambda表达式
        /*int num = change(str, (String s) -> {
            return s.split(",")[1];
        }, (String s) -> {
            return Integer.parseInt(s);
        }, (Integer i) -> {
            return i + 100;
        });*/

        //优化Lambda
        int num = change(str, s -> s.split(",")[1], s ->Integer.parseInt(s),i-> i + 100);
        System.out.println(num);
    }
}

Program demonstration:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109146089