4-1 Java新特性玩转JDK8 函数式编程Function

4-1 Java新特性玩转JDK8 函数式编程Function


如果您还没有阅读本章须知,请先阅读➡ 本章须知


资料

  • Function 为函数型接口,有参数,有返回值。
    @FunctionalInterface 接口用处是告诉其他团队成员该接口是一个函数式接口,有且只能有一个需实现的方法
//@param <T> 是方法传入参数
//@param <R> 是方法返回类型

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

}

作用

作用:将转换逻辑提取出来,解耦合。

示例

将传入参数扩大100倍后返回

public class Main {
    public static void main(String[] args) throws Exception {
        // Function<返回值类型,参数类型>
        Function<Integer, Integer> func = p -> p * 100;
        System.out.println(func.apply(100));
        // 输出 10000
    }
}

原文地址:
https://www.yuque.com/books/share/94d7e9ce-40a5-4878-97f2-c6297f679886

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值