147-Function接口

java.util.function.Function<T,R>接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件
后者称为后置条件
Function接口最主要的是抽象方法:R apply(T t),根据类型T的参数获取类型R的结果
使用场景例如:将String类型转换为Integer类型
package Demo19;

import java.util.function.Function;

/*
java.util.function.Function<T,R>接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件
后者称为后置条件
Function接口最主要的是抽象方法:R apply(T t),根据类型T的参数获取类型R的结果
使用场景例如:将String类型转换为Integer类型
 */
public class Demo14Function {
    /*
    定义一个方法
    方法的参数传递一个字符串类型的整数
    方法的参数传递一个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); 自动拆箱将Integer->int类型
        System.out.println(in);
    }

    public static void main(String[] args) {
        //定义一个字符串类型的整数
        change("1234",(String s)->{
            return Integer.parseInt(s); //Integer类中有个parseInt方法将字符串类型数据转换为Integer类型
        });
    }
}

2.Function类的andThen

Function接口的默认方法andThen用来进行组合操作
需求:
  把String类型的“123”,转换为Integer类型,把转换后的结果加上10,
  再把增加之后的Integer类型的数据转换为String类型
  两次转换 String->Integer->String
分析:
 转换了两次
   第一次是把String类型转换为Integer类型 可以用Function接口来转换
   所以我们可以使用  Function<String Integer> fun1
    Integer i =  fun1.apply("123")+10;

   第二次转换是把Integer类型转换为String类型
   所以我们可以使用Function<Integer,String> fun2
    String s = fun2.apply(i);

    我们可以使用andThen方法把两次转换组合在一起
    fun1.andThen(fun2).apply("123)
    流程是fun1先调用apply方法 String->Integer
     fun2再调用apply方法Integer->String
package Demo19;

import java.util.function.Function;

/*
Function接口的默认方法andThen用来进行组合操作
需求:
  把String类型的“123”,转换为Integer类型,把转换后的结果加上10,
  再把增加之后的Integer类型的数据转换为String类型
  两次转换 String->Integer->String
分析:
 转换了两次
   第一次是把String类型转换为Integer类型 可以用Function接口来转换
   所以我们可以使用  Function<String Integer> fun1
    Integer i =  fun1.apply("123")+10;

   第二次转换是把Integer类型转换为String类型
   所以我们可以使用Function<Integer,String> fun2
    String s = fun2.apply(i);

    我们可以使用andThen方法把两次转换组合在一起
    fun1.andThen(fun2).apply("123)
    流程是fun1先调用apply方法 String->Integer
     fun2再调用apply方法Integer->String
 */
public class Demo15AndThen {
    public static void change(String s , Function<String,Integer> fun1, Function<Integer,String> fun2){
        String ss = fun1.andThen(fun2).apply(s);
        System.out.println(ss);
    }

    public static void main(String[] args) {
        change("123",(String s )->{
            //  把String类型的“123”,转换为Integer类型,把转换后的结果加上10,
            return Integer.parseInt("123")+10;
        },(Integer i )->{
            //  再把增加之后的Integer类型的数据转换为String类型
            return i+""; //Integer类型转换为字符串
        });
    }

}

3.Function接口练习 -自定义函数模型拼接

练习:自定义函数的拼接
题目:
请使用Function进行函数模型的拼接,按照顺序需要执行的多个函数操作为
    String str="赵丽颖,20"
  1.将字符串截取数字年龄部分,得到字符串
    Function<String,String> "赵丽颖,20"->"20"
  2.将上一步的字符串转换为int类型的数字
    Function<String,Integer> "20"->20
  3.将上一步的int数字累加100,得到结果int数字
    Function<Integer Integer> 20->120
package Demo19;

import java.util.function.Function;

/*
练习:自定义函数的拼接
题目:
请使用Function进行函数模型的拼接,按照顺序需要执行的多个函数操作为
    String str="赵丽颖,20"
  1.将字符串截取数字年龄部分,得到字符串
    Function<String,String> "赵丽颖,20"->"20"
  2.将上一步的字符串转换为int类型的数字
    Function<String,Integer> "20"->20
  3.将上一步的int数字累加100,得到结果int数字
    Function<Integer Integer> 20->120
 */
public class Demo16Test {
    public static void method(String str, Function<String,String> fun1,Function<String,Integer> fun2,Function<Integer,Integer> fun3){
        int i = fun1.andThen(fun2).andThen(fun3).apply(str);
        System.out.println(i);
    }

    public static void main(String[] args) {
        String s="赵丽颖,20";
        method(s,(String str)->{
            return str.split(",")[1];
        },(String str)->{
            return Integer.parseInt(str);
        },(Integer i)->{
            return i+100;
        });
    }
}

输出120

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值