JAVA learning - functional interface, Supplier, Consumer, Predicate, Function

functional interface

For an interface with one and only one abstract method, you can use the Lambda interface;
you can write @FunctionalInterface in the header to check and confirm

If the return value of the method is a functional interface, you can use a Lambda expression to return it as the result;

Supplier interface

@FunctionalInterface
public interface Supplier
represents the result supplier.
There is no requirement that a new or different result be returned each time the provider is called.

This is a functional interface whose functional method is get().

Consumer interface

@FunctionalInterface
public interface Consumer
represents an operation that accepts a single input parameter and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side effects.
This is a functional interface whose function method is accept(Object).

Predicate

@FunctionalInterface
public interface Predicate
represents a predicate (boolean-valued function) of one parameter.
This is a functional interface whose functional method is test(Object).

boolean test​(T t) Evaluates this predicate on the given arguments.
default Predicate negate​() Returns a predicate representing the logical negation of this predicate.
Description
default Predicate and​(Predicate<? super T> other)
returns a combined predicate representing the short-circuiting logical AND of this predicate with another predicate.
default Predicate or​(Predicate<? super T> other)
returns a combined predicate representing the short-circuit logical OR of this predicate with another predicate.

Function

Interface Function<T,R>
T - the type of function input
R - the type of function result
public interface Function<T,R>
represents a function that accepts one parameter and produces a result.

R apply​(T t) applies this function to the given arguments.
default Function<T,V> andThen​(Function<? super R,? extends V> after) returns a composed function that first applies the function to its input and then applies the after function to the result.

Guess you like

Origin blog.csdn.net/weixin_52723971/article/details/111483890