lambda vs anonymous class

Viewed 1004

I have the following piece of code:

eventBus.subscribe(new EventBusListener<NavigationEvent>() {
    @Override
    public void onEvent(Event<NavigationEvent> event) {
        event.getPayload();
    }
});

eventBus.subscribe(new EventBusListener<NotificationEvent>() {
    @Override
    public void onEvent(Event<NotificationEvent> event) {
        event.getPayload();
    }
});

IntelliJ tells me that I could replace those two anonymous classes by lambda expressions, like:

eventBus.subscribe((EventBusListener<NavigationEvent>) Event::getPayload);
eventBus.subscribe((EventBusListener<NotificationEvent>) Event::getPayload);

Compilation works well but at runtime the application crashes with the following error: java.lang.IllegalArgumentException: Could not resolve payload type caused by the getPayload() of Event<T> class.

What am i missing with lamdbas and generics?

1 Answers
Related