I have once read the following code from a book about method references.
File[] hiddenFiles = new File(".").listFiles(File::isHidden)
When I look up from the File API for the listFiles method, I see it only has the following methods:
listFiles(FileFilter filter)
listFiles(FilenameFilter filter)
I have tried the code and it works. But while the API states it accepts either FileFilter or FilenameFilter, why the code can work ?
My understanding of File::isHidden is that it is equivalent to the following lambdas:
(File file) -> file.isHidden()
But in FileFilter, the method that need to specify is following.
boolean accept(File pathname)
Then shouldn't there be a method named accept defined there, like:
File[] hiddenFiles = new File(".").listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isHidden();
}
});
Or the compiler can somehow automatically detect the pattern and regard the code as a FileFilter, although the method "accept" is not defined and a FileFilter object is not created ?