Java Lambda Method Reference – Clarity Required

Java can’t check the correct types because they are not part of the interface definition but part of the method definition. It works using the following adjustments:

ParsingFileFunction.java

public interface ParsingFileFunction<K, T> {
    K getMessage(T inputMessage);
}

Note that the types have been moved to the interface definition.

SomeService.java

import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class SomeService {

    public Messages getMessage(ParsingFileFunction<Messages, File> parse, File file) {
        return parse.getMessage(file);
    }
}

Note the explicit types in the declaration of file.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top