“listFiles”的方法引用

huangapple go评论103阅读模式
英文:

Method references of listFiles

问题

我曾经从一本关于方法引用的书中阅读过以下代码。

File[] hiddenFiles = new File(".").listFiles(File::isHidden);

当我查阅listFiles方法的文件API时,我发现它只有以下几种方法:

listFiles(FileFilter filter);

listFiles(FilenameFilter filter);

我尝试过这段代码,它可以工作。但是尽管API说明可以接受FileFilterFilenameFilter,为什么这段代码能够工作呢?

我对File::isHidden的理解是,它等同于以下的lambda表达式:

(File file) -> file.isHidden();

但在FileFilter中,需要指定的方法如下。

boolean accept(File pathname);

那么在这里不应该有一个名为accept的方法吗,就像这样:

File[] hiddenFiles = new File(".").listFiles(new FileFilter() {
 public boolean accept(File file) {
   return file.isHidden();
 }
});

或者编译器是否可以自动检测模式,并将代码视为FileFilter,尽管方法accept未被定义且未创建FileFilter对象?

英文:

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 ?

答案1

得分: 4

以下是翻译好的内容:

有几个有趣的部分涉及到这个问题:

  • 问题:什么是 isHidden,为什么它可以作为 File.listFiles() 的一个可接受参数?

    https://www.geeksforgeeks.org/file-ishidden-method-in-java-with-examples

    isHidden() 函数是 Java 中 File 类的一部分。此函数确定由抽象文件名表示的文件或目录是否是隐藏的。如果抽象文件路径是隐藏的,则该函数返回 true,否则返回 false。

  • 问题:什么是 File::isHidden 的语法?

    "双冒号" 是 方法引用。它在 Java 8 及更高版本中新增。

  • 问题:那么为什么 isHidden() 是可接受的 FileFilter 参数呢?

    https://docs.oracle.com/javase/8/docs/api/java/io/FileFilter.html

    这是一个函数式接口,因此可以用作 lambda 表达式或方法引用的赋值目标。

    File::isHidden 是一个返回 "true" 或 "false" 的 lambda 表达式。

英文:

There are actually several interesting parts to this question:

  • Q: What is isHidden, and why is it a permissible argument to File.listFiles()?

    > https://www.geeksforgeeks.org/file-ishidden-method-in-java-with-examples
    >
    > The isHidden() function is a part of File class in Java . This
    > function determines whether the is a file or Directory denoted by the
    > abstract filename is Hidden or not.The function returns true if the
    > abstract file path is Hidden else return false.

  • Q: What is the File::isHidden syntax?

    The "double colon" is a method reference. It's new with Java 8 and higher.

  • Q: So why is isHidden() an acceptable FileFilter parameter?

    > https://docs.oracle.com/javase/8/docs/api/java/io/FileFilter.html
    >
    > This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    File::isHidden is a lambda expression that returns "true" or "false".

huangapple
  • 本文由 发表于 2020年10月18日 09:19:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64408835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定