Lambda in Java work in combination with the notion of functional interface.
The typical example is Function
. Function
is an functional interface whose functional method, apply
, is a method that takes a single argument and return a result.
You can create your own functional interface, who would define a functional method taking 4 parameters and having no return type, like this:
@FunctionalInterface
interface RectangleDrawer {
void draw(double x, double y, double w, double h);
}
(The FunctionalInterface
annotation is not strictly necessary but it gives a clear intent).
You can then create a lambda that comply with the contract of this functional interface. The typical lambda syntax is (method arguments) -> (lambda body)
. In this example, it would be: (x, y, w, h) -> gc.fillRect(x, y, w, h)
. This compiles because the lambda declares 4 arguments and has no return type, so it can be represented as the functional method of RectangleDrawer
defined before.
You example would become:
static GraphicsContext gc;
public static void main(String[] args) {
draw(0, 0, 50, 50, (x, y, w, h) -> gc.fillRect(x, y, w, h));
draw(0, 0, 50, 50, (x, y, w, h) -> gc.strokeRect(x, y, w, h));
}
static void draw(double x, double y, double w, double h, RectangleDrawer drawer) {
drawer.draw(x, y, w, h);
}
In this particular case, it is possible to use a method reference to create the lambda, using the ::
operator, which allows to write simpler code:
static GraphicsContext gc;
public static void main(String[] args) {
draw(0, 0, 50, 50, gc::fillRect);
draw(0, 0, 50, 50, gc::strokeRect);
}
static void draw(double x, double y, double w, double h, RectangleDrawer drawer) {
drawer.draw(x, y, w, h);
}