Java 8之Function接口方法应用

1、什么是Function接口?

在java8以后的接口可以有接口方法的默认实现了,Function源代码如下:
@FunctionalInterface
public interface Function<T, R> {

	//将参数赋予给相应方法,传入T,返回R
    R apply(T t);

	//先执行参数,再执行调用者 两个Function,先执行后面的,再执行前面的
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

	//先执行调用者,再执行参数,和compose相反。先执行第一个fun1,再接着执行后面的fun2
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

	//返回当前正在执行的方法
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}
2、具体使用例子代码如下:

apply方法使用,传入字符串,返回一个字符串:

/**
 * apply 传入字符串,返回一个字符串
 */
public class FunctionApply {

    public static void main(String[] args) {
        functionApply();
    }

    /**
     * 传入参数字符串,返回参数字符串。字符串转大小写
     */
    public static void functionApply () {
        String result = typeConvert("amy", String::toUpperCase);
        //function --> AMY
        System.out.printf("function --> {%s}%n",  result);
    }

    /***
     * Function apply接口  传入一个参数,返回值一个参数
     */
    public static <R> R typeConvert (String str, Function<String, R> function) {
        return function.apply(str);
    }

}

andThen方法,先执行fun1,再执行fun2

/**
 * andThen 先执行fun1,再执行fun2
 */
public class FunctionAndThen {

    public static void main(String[] args) {
        functionAndThen();
    }

    /**
     * 先将字符串100转换未Integer类型,再Integer + 10得到结果 110
     */
    public static void functionAndThen() {
        Integer result = typeConvert("100", Integer::parseInt, s2 -> (s2 + 10));
        //function --> 110
        System.out.printf("function --> {%s}%n", result);
    }

    /**
     * 先执行fun1,再执行fun2 传入参数str
     */
    public static <R> R typeConvert(String str, Function<String, Integer> fun1, Function<Integer, R> fun2) {
        return fun1.andThen(fun2).apply(str);
    }

}

compose方法,先执行fun2,再执行fun1 跟andThen相反

/**
 * compose 先执行fun2,再执行fun1 跟andThen相反
 */
public class FunctionCompose {

    public static void main(String[] args) {
        functionCompose();
    }

    /**
     * 先执行fun2 100*100 转换为String类型 再执行fun1把String转换为BigDecimal
     */
    public static void functionCompose() {
        BigDecimal amount = typeConvert2(100, BigDecimal::new, s2 -> String.valueOf(s2 * s2));
        //function --> 10000
        System.out.printf("function --> {%s}%n", amount);
    }


    /**
     * 传入number 先执行fun2 Integer转String 再执行fun1 String 转泛型R
     */
    public static <R> R typeConvert2(Integer number, Function<String, R> fun1, Function<Integer, String> fun2) {
        return fun1.compose(fun2).apply(number);
    }

}

 identity方法,数值为本身 s -> s 替换为 Function.identity()

/**
 * identity 数值为本身 s -> s 替换为 Function.identity()
 */
public class FunctionIdentity {

    public static void main(String[] args) {
        identity();
    }

    /**
     * 把List转为Map, s -> s 表示map的key值,key的数值为本身,可以简写为
     */
    public static void identity() {
        List<String> strings = Arrays.asList("abc", "de");
        //数值为本身 Function.identity() 替换 s -> s,Collectors.toMap(s -> s, String::length));
        Map<String, Integer> map = strings.stream().collect(Collectors.toMap(Function.identity(), String::length));
        //function --> {de=2, abc=3}
        System.out.printf("function --> {%s}%n", map);
    }

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烟雨忆南唐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值