-
Notifications
You must be signed in to change notification settings - Fork 2
Towards Declarative Programming
Predicate
: takes an argument and returns a boolean
.
Function
: argument and return types differ.
Supplier
: takes no argument and returns a value.
Consumer
: takes an argument and returns nothing.
Interface | Function Signature | Example |
---|---|---|
Predicate<T> |
boolean test(T t) |
Collection::isEmpty |
Function<T,R> |
R apply(T t) |
Arrays::asList |
Supplier<T> |
T get() |
Math::random |
Consumer<T> |
void accept(T t) |
System.out::println |
There are three variants of each of the above interfaces to operate on the primitive types int
, long
and double
. Their names are derived from the basic interfaces by prefixing then with a primitive type.
Examples: IntPredicate
, LongFunction<int[]>
, DoubleSupplier
.
For use when the result type is primitive.
- If both the sources and result types are primitive, prefix
Function
with SrcToResult, for exampleLongToIntFunction
. - If the source is a primitive and the result is an object reference, prefix
Function
with <Src>ToObj, for exampleDoubleToObjFunction
.
BiPredicate<T,U>
, BiFunction<T,U,R>
, and BiConsumer<T,U>
.
-
There are also
BiFunction
variants returning the three relevant primitive types:ToIntBiFunction<T,U>
,ToLongBiFunction<T,U>
andToDoubleBiFunction<T,U>
. -
There are two-argument variants of Consumer that take one object reference and one primitive type:
ObjDoubleConsumer<T>
,ObjIntConsumer<T>
andObjLongConsumer<T>
.
BooleanSupplier
, a variant of Supplier
that returns boolean
values.