Lambda Introduction

As Javacord heavily benefits from lambda expressions, this introduction gives you a quick overview, if you are not familiar with lambdas. Lambdas are used as a short form of functional interface implementations. Functional interfaces, are basically just interfaces with a single method definition. All listeners in Javacord, for example, are functional interfaces and look like this internally (simplified):

/**
 * This listener listens to message creations.
 */
@FunctionalInterface
public interface MessageCreateListener {

    /**
     * This method is called every time a message is created.
     *
     * @param event The event.
     */
    void onMessageCreate(MessageCreateEvent event);

}

Before Java 8, you would have implemented this kind of listener as an anonymous class, which would look like this:

api.addMessageCreateListener(new MessageCreateListener() {
    @Override
    public void onMessageCreate(MessageCreateEvent event) {
        // Do stuff
        event.deleteMessage();
    }
});

In Java 8, this can be replaced with a lambda expression, which does exactly the same thing, but shortens your code. The method parameter (in this case event) is written in front of the -> arrow, and the method body is written after it.

api.addMessageCreateListener(event -> {
    // Do stuff
    event.deleteMessage();
});

Hint: If the method has more than one parameter, it would look like this: (param1, param2) -> { ... }

There’s even a shorter version: if you are only executing one statement, you can get rid of the { } brackets as well:

api.addMessageCreateListener(event -> event.deleteMessage());

However, the above method can be shortened even more, by replacing the lambda expression with a so called “method reference”.

api.addMessageCreateListener(MessageEvent::deleteMessage);

There are also plenty classes in Java 8, which benefit from lambda expressions. One example would be the Optional class, which is explained here.



Improve this page