Java Reference
In-Depth Information
Using Lambda Predefined Interfaces Instead of Your Own
Problem
You want to use existing interfaces, instead of defining your own, for use with Lambdas.
Solution
Use the Java 8 lambda Functional interfaces from
java.util.function
.
Discussion
In
Using Lambdas/Closures Instead of Inner Classes
,
we used the interface method
ac-
ceptCamera()
defined in the interface
CameraAcceptor
. Acceptor-type methods are quite
common, so the package
java.util.function
includes the
Predicate<T>
interface, which
we can use instead of
CameraAcceptor
. This interface has only one method—
boolean
test(T t)
:
interface
interface
Predicate
Predicate
<
T
> {
boolean
boolean
test
(
T t
);
}
This package includes about 50 of the most commonly needed functional interfaces, such as
IntUnaryOperator
, which takes one
int
argument and returns an
int
value;
LongPredic-
ate
, which takes one
long
and returns
boolean
; and so on.