Function<String, String> function1 = s -> "Hello, "+s;
Function<String, String> function2 = s -> "my name is "+s;
String result = function1.compose(function2).apply("zhangSan");
// Hello, my name is zhangSan
System.out.println(result);
1-3、andThen
示例:andThen(先执行function1 后执行function2)
Function<String, String> function1 = s -> "my name is "+s;
Function<String, String> function2 = s -> "Hello, "+s;
String result = function1.andThen(function2).apply("zhangSan");
// Hello, my name is zhangSan
System.out.println(result);
package java.util.function;
import java.util.Objects;
/**
* Represents a function that accepts one argument and produces a result.
* 表示接受一个参数并产生结果的函数
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #apply(Object)}.
* 这是一个functional interface,它的功能方法是apply(Object) 。
*
* @param <T> the type of the input to the function 函数输入的类型
* @param <R> 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.
* 返回一个组合函数,首先将before函数应用于其输入,然后将此函数应用于结果。
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
* 如果任一函数的评估引发异常,则将其转发给组合函数的调用者。
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function 输入到 before函数的类型,并且组合函数
* @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 一个组合函数首先应用 before函数,然后应用此功能
* @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.
* 返回一个组合函数,首先将此函数应用于其输入,然后将after函数应用于结果。
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
* 如果任一函数的评估引发异常,则将其转发给组合函数的调用者。
*
* @param <V> 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 一个组合函数首先应用此函数,然后应用 after函数
* @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 <T> 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;
}
}