JDK8新特性--常用函数接口--常用的函数式接口_Function接口三

JDK8新特性--常用函数接口--常用的函数式接口_Function接口三

package com.itheima.demo07.Function;

import java.util.function.Function;

/*

练习:自定义函数模型拼接
题目
请使用Function进行函数模型的拼接,按照顺序需要执行的多个函数操作为:
    String str = "赵丽颖,20";

分析:
1. 将字符串截取数字年龄部分,得到字符串;
    Function "赵丽颖,20"->"20"
2. 将上一步的字符串转换成为int类型的数字;
    Function "20"->20
3. 将上一步的int数字累加100,得到结果int数字。
    Function 20->120

*/
public class Demo03Test {

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

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

}

你可能感兴趣的:(java)