Function中没有具体的操作,具体的操作需要我们去为它指定,apply具体返回的结果取决于传入的lambda表达式。
源码
@FunctionalInterface
public interface Function {
/**
* 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 Function 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 Function 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 Function identity() {
return t -> t;
}
}
计算实例代码演示
public static void main(String []args){
function1 test1 = new function1();
//传递行为
System.out.println(test1.compute(1, value -> {return 2 * value;}));
System.out.println(test1.compute(2, value -> 5 + value));
System.out.println(test1.compute(3, value -> value * value));
System.out.println(test1.convert(5, value -> String.valueOf(value + "helloworld")));
System.out.println(test1.method1(2));
Function function = value -> value * 2;
System.out.println(test1.compute(4, function));
Function A=i->i+1;
Function B=i->i*i;
System.out.println("F1:"+B.apply(A.apply(5)));
System.out.println("F2:"+A.apply(B.apply(5)));
}
//传递行为,根据行为选择处理。
public int compute(int a, Function function) {
int result = function.apply(a);
return result;
}
public String convert(int a, Function function) {
return function.apply(a);
}
//之前做法
public int method1(int a) {
return 2 * a;
}
public int method2(int a) {
return 5 + a;
}
public int method3(int a) {
return a * a;
}
compose和andthen的使用
public class function3 {
public static void main(String[] args) {
function3 test = new function3();
System.out.println(test.compute1(2, value -> value * 3, value -> value * value)); // 12
System.out.println(test.compute2(2, value -> value * 3, value -> value * value)); // 36
System.out.println(test.compute3(1, 2, (value1, value2) -> value1 + value2));
System.out.println(test.compute3(1, 2, (value1, value2) -> value1 - value2));
System.out.println(test.compute3(1, 2, (value1, value2) -> value1 * value2));
System.out.println(test.compute3(1, 2, (value1, value2) -> value1 / value2));
System.out.println(test.compute4(2, 3, (value1, value2) -> value1 + value2, value -> value * value)); //25
}
//先运行function2得到结果再运行function1
public int compute1(int a, Function function1, Function function2) {
return function1.compose(function2).apply(a);
}
//先运行function1得到结果再运行function2
public int compute2(int a, Function function1, Function function2) {
return function1.andThen(function2).apply(a);
}
//两参数
public int compute3(int a, int b, BiFunction biFunction) {
return biFunction.apply(a, b);
}
//先运行biFunction得到结果再运行function
public int compute4(int a, int b, BiFunction biFunction,
Function function) {
return biFunction.andThen(function).apply(a, b);
}
}
?