JAVA学习-函数式接口,Supplier,Consumer,Predicate,Function

函数式接口

有且仅有一个抽象方法的接口,可以使用Lambda接口;
可以在头部写@FunctionalInterface,来检测确认

如果方法的返回值一个函数式接口,可以使用Lanbda表达式作为结果返回;

Supplier接口

@FunctionalInterface
public interface Supplier
代表结果供应商。
没有要求每次调用供应商时都会返回新的或不同的结果。

这是一个functional interface的功能方法是get() 。

Consumer接口

@FunctionalInterface
public interface Consumer
表示接受单个输入参数并且不返回结果的操作。 与大多数其他功能接口不同, Consumer预期通过副作用进行操作。
这是一个functional interface的功能方法是accept(Object) 。

Predicate

@FunctionalInterface
public interface Predicate
表示一个参数的谓词(布尔值函数)。
这是一个functional interface,其功能方法是test(Object) 。

boolean test​(T t) 在给定的参数上评估这个谓词。
default Predicate negate​() 返回表示此谓词的逻辑否定的谓词。
描述
default Predicate and​(Predicate<? super T> other)
返回一个组合的谓词,表示该谓词与另一个谓词的短路逻辑AND。
default Predicate or​(Predicate<? super T> other)
返回一个组合的谓词,表示该谓词与另一个谓词的短路逻辑或。

Function

Interface Function<T,R>
T - 函数输入的类型
R - 函数结果的类型
public interface Function<T,R>
表示接受一个参数并产生结果的函数。

R apply​(T t) 将此函数应用于给定的参数。
default Function<T,V> andThen​(Function<? super R,? extends V> after) 返回一个组合函数,首先将该函数应用于其输入,然后将 after函数应用于结果。

猜你喜欢

转载自blog.csdn.net/weixin_52723971/article/details/111483890
今日推荐