Predicate:接收参数T对象,返回一个Boolean类型结果。
Predicate predicate = Object object) -> {
return "Demo".equals(object);
}
boolean test = predicate.test("Demo");
Consumer:接收参数T对象 没有返回值。
Consumer consumer = (Object object) -> {
System.out.println("Demo" + object.toString());
};
consumer.accept("Demo");
Function:接收参数T对象,返回R对象。
Function<String, Integer> function = (String s) -> {
return "Demo".equals(s) ? 1 : 0;
};
function.apply("Demo");
Supplier:不接受参数,直接通过get()获取指定类型的对象。
Supplier<String> supplier = () -> {
return UUID.randomUUID().toString();
};
String s = supplier.get();
UnaryOperator:接收参数T对象,执行业务处理后,返回更新后的T对象。
UnaryOperator<Integer> unaryOperator = (Integer i) ->{
return i++;
};
Integer unaryOperatorApply = unaryOperator.apply(1);
BinaryOperator:接收两个T对象,执行业务处理后,返回一个T对象。
BinaryOperator<Integer> binaryOperator = (Integer i1, Integer i2) -> {
return Math.max(i1,i2);
};
// 还可以这样写 BinaryOperator<Integer> binaryOperator = Math::max;
Integer binaryOperatorApply = binaryOperator.apply(1, 2);
如果本篇文章对你有帮助,还请一键三连【开心】