java function 使用_Java8函数式接口Function的用法

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);

}

}

?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值