当前位置:首页 > IT技术 > 编程语言 > 正文

JAVA中Function的使用
2022-04-25 22:55:36

package com;

import java.util.function.Function;

public class FuctionDemo {
    public static void main(String[] args) {
    convert("100",s -> Integer.parseInt(s),integer ->String.valueOf(integer+100));
    }
    private static void convert(String s, Function<String,Integer> fun1,Function<Integer,String> fun2){
//        Integer s1 = fun1.apply(s);
//        String s2 = fun2.apply(s1);
//        System.out.println(s2);
        String apply = fun1.andThen(fun2).apply(s);
        System.out.println(apply);
    }
}
package com;

import java.util.function.Function;

/**
 * 练习,String s  = "张三,30";
 * 1,将字符串截取得到数字年龄部分
 * 2,将上一步的年龄字符串转换成int类型数据
 * 3,将上一步的int数据加70,得到一个int结果,在控制台输出
 */
public class FuctionDemo2 {
    public static void main(String[] args) {
        convert("张三,30",s -> s.split(",")[1],Integer::parseInt,integer -> integer+70);
        convert("张三,30",s -> Integer.parseInt(s.split(",")[1]),integer -> integer+70);
        convert("张三,30",s -> Integer.parseInt(s.split(",")[1])+70);

    }
    public static void convert(String s, Function<String,String>fun1,Function<String ,Integer>fun2,Function<Integer,Integer>fun3){
        Integer apply = fun1.andThen(fun2).andThen(fun3).apply(s);
        System.out.println(apply);
    }
    public static void convert(String s, Function<String,Integer>fun1,Function<Integer ,Integer>fun2){
        Integer apply = fun1.andThen(fun2).apply(s);
        System.out.println(apply);
    }
    public static void convert(String s, Function<String,Integer>fun1){
        Integer apply = fun1.apply(s);
        System.out.println(apply);
    }
}

 

本文摘自 :https://www.cnblogs.com/

开通会员,享受整站包年服务立即开通 >