/**
* 表示一个函数,它接收一个参数并且返回一个结果。
* 这是一个函数式接口,它有一个重要的方法是apply(Object)
* @param <T> 函数的输入类型
* @param <R> 函数结果的类型
* @since 1.8
*/
@FunctionalInterface
public interface Function<T, R> {
/**
* 将该方法应用到指定的参数上
* @param t 函数参数
* @return 返回函数的结果
*/
R apply(T t);
/**
* 返回一个组合函数,首先使用before给它的输入参数,接着将前面的结果应用给此函数。
* 如果在评测期间抛出任何异常,都会由调用者返回给组合函数。
*
* @param <V> 组合函数中,before 函数的输入类型
* @param before 该函数应该在当前函数引用之后在应用。
* @return 一个组合函数第一次被应用before函数
* function and then applies this function
* @throws 如果before为null,就会抛出NullPointerException异常
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
//将第一个function传进计算before.apply,然后将结果作为第二次传入的值,
// 对第二个function进行apply
return (V v) -> apply(before.apply(v));
}
/**
* 返回一个组合函数,第一个使用该函数给它的输入,接着对其结果使用after函数。
* 如果评测期间有任何的异常抛出,都将会有调用者抛给组合函数。
* @param <V> after函数和组合函数的输出类型
* @param after 当前函数应用之后再应用该函数
* @return 返回一个组合函数,第一次应用该函数第二次应用after函数
* @throws 如果after为null,则会抛出NullPointerException异常
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* 返回一个函数总返回自己的输入参数
*
* @param <T> 函数的输入和输出对象类型
* @return 返回一个函数总返回自己的输入参数
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
方法实践:
package com.java8.learn;
import java.util.function.Function;
/**
*@author:liyongyong
*@description: 函数表达式应用,传表达式或者行为
*@date 2019/1/23
*/
public class FunctionTest {
public static void main(String[] args){
FunctionTest test = new FunctionTest();
System.out.println(test.compute(1,value->2*value));
System.out.println(test.compute(2,value -> 3 + value));
System.out.println(test.compute(3,value -> value * value));
System.out.println(test.convertToString(4,value->String.valueOf(value+"hello")));
Function<Integer,Integer> id = Function.identity();
System.out.println(id.apply(3));
}
public int compute(int num, Function<Integer,Integer> function){
int result = function.apply(num);
return result;
}
public String convertToString(int num,Function<Integer,String> function){
String result = function.apply(num);
return result;
}
}
结果输出:
2
5
9
4hello
3
package com.java8.learn;
import java.util.function.Function;
/**
*@author:liyongyong
*@description: Function接口compose和andThen方法学习
*@date 2019/1/23
*/
public class FunctionTest2 {
public static void main(String[] args){
FunctionTest2 function = new FunctionTest2();
System.out.println(function.compute(3,value->value * 3,value->value*value));
System.out.println(function.compute1(3,value->value * 3,value->value*value));
}
public int compute(int num, Function<Integer,Integer> f1,Function<Integer,Integer> f2){
//f1调用compose将f2作为参数,那么先执行的f2的方法体也就是value*value,
// 把f2方法体的执行结果作为f1的输入,调用apply方法来执行了value *3
//最后得到结果是3*3*3=27
return f1.compose(f2).apply(num);
}
public int compute1(int num, Function<Integer,Integer> f1,Function<Integer,Integer> f2){
//f1调用andThen将f2作为参数,那么先执行的f1的方法体也就是value*3,
// 把f1方法体的执行结果作为f2的输入,调用apply方法来执行了value *value
//最后得到结果是3*3=9*9=81
return f1.andThen(f2).apply(num);
}
}
27
81