首页 > 其他分享 >函数式接口:Function

函数式接口:Function

时间:2022-08-14 19:37:29浏览次数:43  
标签:Function return 函数 接口 Integer apply public String

Function接口

Function接口在java中主要用来转换类型
通过调用apply方法将T类型转换为R类型

抽象方法:apply

R apply(T var1);

代码样例

public class Main {
    public static void main(String[] args) {
        String str = "13523";
        Integer i = test(str, (t) -> {
            return Integer.parseInt(str);
        });
        System.out.println("i的value: " + i);
    }
    public static <T, R> R test(T t, Function<T, R> f) {
        return f.apply(t);
    }
}

输出结果

i的value: 13523

default方法:andThen

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

代码样例

public class Main {
    public static void main(String[] args) {
        Function<String, Integer> f1 = (t) -> {
            return Integer.parseInt(t);
        };
        Function<Integer, String> f2 = (t) -> {
            return String.valueOf(t);
        };
        String str = "13523";
        String str1 = test(str, f1.andThen(f2));
        System.out.println("str1的value: " + str1);
    }
    public static <T, R> R test(T t, Function<T, R> f) {
        return f.apply(t);
    }
}

输出结果

str1的value: 13523

String -> Integer -> String

default方法:compose

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
    Objects.requireNonNull(before);
    return (v) -> {
        return this.apply(before.apply(v));
    };
}

与andThen刚好相反,先根据入参Function执行类型转换,然后再根据当前Function执行类型转换。

代码样例

public class Main {
    public static void main(String[] args) {
        Function<String, Integer> f1 = (t) -> {
            return Integer.parseInt(t);
        };
        Function<Integer, String> f2 = (t) -> {
            return String.valueOf(t);
        };
        Integer i = 13523;
        Integer i1 = test(i, f1.compose(f2));
        System.out.println("i1的value: " + i1);
    }
    public static <T, R> R test(T t, Function<T, R> f) {
        return f.apply(t);
    }
}

输出结果

i1的value: 13523

Integer -> String -> Integer

其他方法

static <T> Function<T, T> identity() {
    return (t) -> {
        return t;
    };
}
  • identity:返回一个和输入类型相同的输出类型。

标签:Function,return,函数,接口,Integer,apply,public,String
From: https://www.cnblogs.com/buzuweiqi/p/16585935.html

相关文章

  • 函数的形参和实参不匹配会出现什么情况
    知识储备:js的函数参数和C语言等编程语言不同,没有参数重载,实参和形参之间的值传递或者地址传递;有的是js的相同函数名会被后面的参数覆盖,实参和形参的传递都是值传递;实参的......
  • 每五秒获取调取告警接口
     //告警数量 constgetAlarm=()=>{  getAlarmCount().then(   (res:any)=>{    if(res.code==200){     setCount(res......
  • django restframework 后端接口权限
    REST_FRAMEWORK={'EXCEPTION_HANDLER':'djangoProject.utils.exception.custom_exception_handler',#在不需要权限就能访问的视图设置permissions_classes......
  • python 中字符串 内置函数 find
     001、>>>str1="xabdxyabxykk"##测试字符串>>>str1'xabdxyabxykk'>>>str1.find("ab")##返回测试字符串中首次匹配ab的首字符的索......
  • 拷贝构造函数
    c++中的拷贝构造函数调用时机通常有三种情况1.使用一个已经创建完毕的对象来初始化一个新的对象2.值传递的方式给函数参数传值3.以值方式返回局部对象//情况1classP......
  • 常用函数
    一、常用日期函数1.unix_timestamp:返回当前或指定时间的时间戳selectunix_timestamp();selectunix_timestamp("2020-10-28",'yyyy-MM-dd');2.from_unixtime:将时间......
  • hive function汇总
    showfunctions查看了所有的方法把所有的方法记录下来,下次免得去翻别人的博客了数学函数数学常规函数函数简介用法abs绝对值selectabs(-13);13negati......
  • 【问题】没有与指定类型匹配的重载函数
    没有与指定类型匹配的重载函数这个问题的成因不止一个,这里只记述一下我碰到的较为特殊的一种情况。问题描述起因是师弟在实现一个类时,发现定义函数时,函数报出了没有与指......
  • 函数式接口:Predicate
    Predicate接口Predicate在英文中的意思:断言。也就是对事物下判断。抽象方法:test//对指定类型的数据var1使用自定义的方式判定booleantest(Tvar1);其实Predicate就......
  • python 中字符串格式化函数 format()
     001、>>>"{0}".format("xxx")##位置参数'xxx'>>>"{0}.{1}.{2}".format("xxx","yyy","zzz")'xxx.yyy.zzz'>>>"\t{0}.{......