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