java8 函数式编程接口


java8 函数式接口java.util.function.*

1.Function <入参类型,出参类型>
例:
Function<Integer, String> func=p->"num:"+p;
func.apply(9);

2.Predicate(断言) <入参类型>出参:boolean类型
例:
Predicate<Integer> greaterThan7 = x->x>7 ;
greaterThan7(6); //false

3.Consumer(顾客) <入参类型>没有出参
例:
Consumer<String> consumer= p -> System.out.println(p);
或Consumer<String> consumer=System.out::println;
consumer.accept("你好。");

4.Supplier(供货商) <出参类型>没有入参
例:
Supplier<String> supplier=()->"Hello";
supplier.get();

5.BinaryOperator <参数类型> 接口接收两个参数,返回一个值,数据类型相同。
例:
BinaryOperator<Integer> addTwo = (x,y) -> x + y;
addTeo.apply(3,6);

猜你喜欢

转载自www.cnblogs.com/mznsndy/p/11642671.html