Java内置Function参数,类包是在 java.base 模块下 java.util.function 包中,其方法主要用于对一个请求参数的处理,并返回一个结果。
目录
- Function源码
- Function主要方法
- apply方法
- 创建 简单的Function对象
- compose方法、andThen方法和identity方法
- compose方法
- andThen方法
package java.util.function;
import java.util.Objects;
/**
* Represents a function that accepts one argument and produces a result.
*
* This is a functional interface
* whose functional method is {@link #apply(Object)}.
*
* @param the type of the input to the function
* @param the type of the result of the function
*
* @since 1.8
*/
@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);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* Returns a function that always returns its input argument.
*
* @param the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
当前方法就是我们使用匿名函数时需要重写的方法,其中请求参数和返回参数都需要在我们生成Function对象的时候传进去,而apply方法也是这个类最核心的方法。
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
Function是现有43个函数中基础的函数之一,简单的方法可以省略方法体{},和if的写法一样,但是复杂写法不可以省略{}。
/**
* ([参数列表]) ->{
* 代码体;
* }
* 或
* ([参数列表]) ->代码体
*
*/
public static void main(String[] args) {
//Function 传入一个参数,并返回一个参数,两个参数类型需要自己传 可以对数据进行处理
Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1;
System.out.println(stringIntegerAddFunction.apply("100"));
Function<String,Integer> stringIntegerAddFunction2=(str)->{
Integer integer=Integer.parseInt(str);
return integer+1;
};
System.out.println(stringIntegerAddFunction2.apply("100"));
}
使用匿名函数时,匿名函数接口的类中只允许存在一个方法,而之所有这三方法,是因为接口中可以通过关键字default定义默认方法,实现类如果不想要默认方法的实现逻辑可以根据需求重新定义,通过关键字static定义静态方法,实现类如果不想要静态方法的实现逻辑可以根据需求重新定义。
有的时候,我们需要将两个或多个方法进行组合使用,这个时候就需要compose方法,compose会通过从右到左的顺序执行我们拼接的方法。
/**
* ([参数列表]) ->{
* 代码体;
* }
* 或
* ([参数列表]) ->代码体
*
*/
public static void main(String[] args) {
//Function 传入一个参数,并返回一个参数,两个参数类型需要自己传 可以对数据进行处理
Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1;
Function<Integer,String> integerStringFunction=integer -> String.valueOf(integer*2);
System.out.println(stringIntegerAddFunction.compose(integerStringFunction).apply(100));
}
可以通过结果看出,compose拼接会先执行被拼接的函数(integerStringFunction),再回去调用我们拼接的函数(stringIntegerAddFunction),为方便查看,我们请求的apply方法的类型已经发送了改变。
andThen方法则刚好相反,这个函数会把拼接的函数从左到右执行。
/**
* ([参数列表]) ->{
* 代码体;
* }
* 或
* ([参数列表]) ->代码体
*
*/
public static void main(String[] args) {
//Function 传入一个参数,并返回一个参数,两个参数类型需要自己传 可以对数据进行处理
Function<String,Integer> stringIntegerAddFunction=(str)->Integer.parseInt(str)+1;
Function<Integer,String> integerStringFunction=integer -> String.valueOf(integer*2);
System.out.println(stringIntegerAddFunction.andThen(integerStringFunction).apply("100"));
}
根据结果可以看出,andThen拼接会先执行被拼接的函数(stringIntegerAddFunction),再回去调用我们拼接的函数(integerStringFunction),为方便查看,我们请求的apply方法的类型已经发送了改变。