函数式遍程----Function
Consumer定义
@FunctionalInterface public interface Consumer<T> { void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } }
其核心的方法如下:
void accept(T t){
}
default Consumer andThen(Consumer<? super T> after){
}
Consumer在处理的时候是没有返回值
Function<T,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); }
public int compute(int a, Function<Integer, Integer> function) { int result = function.apply(a); return result; }
test.compute(5,value -> value * value) //25 计算平方 test.compute(5,value -> value + value) //10 求和 test.compute(5,value -> value - 2) //3